如何使用 YUI 进行基本的 DOM 操作(清除内容、追加到内容)?

发布于 2024-07-27 16:33:38 字数 338 浏览 3 评论 0原文

我正在用 YUI 做一个项目,我尝试做以前用 jQuery 做的事情。 我发现很难做一些基本操作,即:

  1. 清除 DOM 元素的内容
  2. 附加到 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:

  1. clear content of DOM element
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘过的浮云 2024-08-03 16:33:38

这是 Dom 方法的 YUI 文档。
雅虎! UI库> dom> YAHOO.util.Dom
或者尝试

YAHOO.util.Dom.get("somediv").innerHTML("<div>something</div>");

Here is the YUI documentation for Dom methods.
Yahoo! UI Library > dom > YAHOO.util.Dom
Or try

YAHOO.util.Dom.get("somediv").innerHTML("<div>something</div>");
度的依靠╰つ 2024-08-03 16:33:38

YUI 似乎没有提供与 jQuery 相同级别的 DOM 操作(尽管我还没有使用过 YUI3,所以这可能会改变)。

innerHTML 确实有效,并且曾经更快,尽管我认为这在某些现代浏览器中正在发生变化。 通过标准方法附加内容很容易:

element.appendChild(childElement);

清除则稍微复杂一些。 实际上,我已经扩展了 YUI DOM 实用程序,并将其添加为一种方法:

removeChildren : function(elem)
{
    var theElement = this.get(elem);
    var elemChildren = theElement.childNodes;
    var childCount = elemChildren.length;
    for (var i = childCount -1; i >= 0; i--)
    {
        theElement.removeChild(theElement.childNodes[i]);
    }
}

需要注意的是,在循环子节点时,我从最后一个元素开始,一直到前面。 这是因为每当删除一个节点时,数组的大小就会发生变化。 另外,如果我删除位置 [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:

element.appendChild(childElement);

Clearing is a little more involved. I have actually ended up extending the YUI DOM utility and added this as a method:

removeChildren : function(elem)
{
    var theElement = this.get(elem);
    var elemChildren = theElement.childNodes;
    var childCount = elemChildren.length;
    for (var i = childCount -1; i >= 0; i--)
    {
        theElement.removeChild(theElement.childNodes[i]);
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文