如何使用 YUI 进行基本的 DOM 操作(清除内容、追加到内容)?
我正在用 YUI 做一个项目,我尝试做以前用 jQuery 做的事情。 我发现很难做一些基本操作,即:
- 清除 DOM 元素的内容
- 附加到 DOM 元素的内容
在 jQuery 中,我会这样做:
$(".someSelector").empty().append("<div>something</div>");
如何在 YUI 中执行上述操作? 看起来它在很多方面都遵循 W3C DOM 规范,我对该规范不是很熟悉。 如果我应该使用任何特定的 W3C 或此类材料,请给出具体的链接。
I am doing a project with YUI where I attempt to do things I have previously been doing with jQuery. I am finding it difficult to do some basic operations, and namely:
- clear content of DOM element
- append to content of DOM element
In jQuery, I would do:
$(".someSelector").empty().append("<div>something</div>");
How to do the above in YUI? Seems like it defers to W3C DOM spec in many aspects, I am not very familiar with that spec. If there's any specific W3C or such material I should use, please give specific links.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Dom 方法的 YUI 文档。
雅虎! UI库> dom> YAHOO.util.Dom
或者尝试
Here is the YUI documentation for Dom methods.
Yahoo! UI Library > dom > YAHOO.util.Dom
Or try
YUI 似乎没有提供与 jQuery 相同级别的 DOM 操作(尽管我还没有使用过 YUI3,所以这可能会改变)。
innerHTML 确实有效,并且曾经更快,尽管我认为这在某些现代浏览器中正在发生变化。 通过标准方法附加内容很容易:
清除则稍微复杂一些。 实际上,我已经扩展了 YUI DOM 实用程序,并将其添加为一种方法:
需要注意的是,在循环子节点时,我从最后一个元素开始,一直到前面。 这是因为每当删除一个节点时,数组的大小就会发生变化。 另外,如果我删除位置 [0] 处的节点,则数组的所有元素都会向下移动一个位置(意味着位置 [1] 处的元素现在变成位置 [0] 处的元素)。 如果我从数组的前面开始,我会错过元素,并很快超出数组的边界。
YUI does not seem to provide the same level of DOM manipulation that jQuery does (although I haven't played with YUI3 yet, so this may change).
innerHTML definitely works, and was faster once upon a time, although I think this is changing in some modern browsers. Appending content is easy enough via the standard methods:
Clearing is a little more involved. I have actually ended up extending the YUI DOM utility and added this as a method:
It's important to note than when looping through the child nodes, I start with the last element and work my way to the front. This is because whenever a node is removed, the size of the array changes. Also, if I remove the node at position [0], all the elements of the array move down a position (meaning the element that was at position [1] now becomes the element at position [0]). If I was to start at the front of the array, I would miss elements, and quickly exceed the array's boundary.