简体   繁体   English

为什么我不能包装一系列 <li> 在带有javascript的代码中?

[英]Why can't I wrap a series of <li>'s in a tag with javascript?

I know how to do this with jQuery, but I want to learn it with vanilla javascript, I have a feeling it'll be faster. 我知道如何用jQuery做到这一点,但是我想用香草javascript学习,我觉得它会更快。 Here's a link to jsFiddle with a stripped down version of what I'm trying to do. 这是jsFiddle的链接,其中包含我正在尝试做的精简版本。 http://jsfiddle.net/yramagicman/MyM2B/ and below is the offending script. http://jsfiddle.net/yramagicman/MyM2B/及以下是有问题的脚本。

<script>
var id = 'ul';
var tag = 'li';
var wrapTag = 'span';
var wrapper = document.createElement(wrapTag);
var z = document.getElementById(id);
var y = z.getElementsByTagName(tag);
var count = y.length;
for (var i = 0; i < count; i++) {
    y[i].setAttribute("class", "red"); //so I can see the result
    wrapper.appendChild(y[i].cloneNode(true));
    y[i].parentNode.replaceChild(wrapper, y[i]);
    wrapper.setAttribute('class', 'hi'); //so I can see the result
}</script>
<!-- and the html-->
<ul id="ul" class="ul">
    <li class="li">1</li>
    <li class="li">2</li>
    <li class="li">3</li>
    <li class="li">4</li>
    <li class="li">5</li>
    <li class="li">6</li>
    <li class="li">7</li>
    <li class="li">8</li>
</ul>

I can wrap the entire Ul in a div or span or whatever just fine but when I try to wrap each list item I get the error "HIERARCHY_REQUEST_ERR: DOM Exception 3: A Node was inserted somewhere it doesn't belong." 我可以将整个Ul封装为div或span或其他任何形式,但是当我尝试封装每个列表项时,出现错误“ HIERARCHY_REQUEST_ERR:DOM异常3:在不属于该节点的位置插入了一个Node”。 Any ideas? 有任何想法吗?

flem's answer is not correct. flem的答案不正确。 Although an <li> element is not valid as child of a <span> element in any version of HTML, it should not be an issue for the DOM and in fact is not. 尽管<li>元素在任何版本的HTML中都不能作为<span>元素的子元素有效,但对于DOM来说应该不是问题,实际上不是。

There are two key pieces of information you need to understand. 您需要了解两个关键信息。 The first is that the node list returned by getElementsByTagName is a live list attached to the DOM document. 第一个是getElementsByTagName返回的节点列表是附加到DOM文档的活动列表。 If the JS loop changes the DOM document, then the list gets updated. 如果JS循环更改DOM文档,则列表将更新。

The second is that you are creating the wrapper element once but using it with replaceChild each time around the loop. 第二个是您只创建一次包装器元素,但是每次循环时都将其与replaceChild一起使用。

So the first time around the loop everything works fine. 因此,第一次在循环中一切正常。 The second time around the loop, the cloned element gets added to the wrapper, which is now in the DOM document and so becomes the second element in the y node list. 循环第二次,将克隆的元素添加到包装器中,该包装器现在在DOM文档中,因此成为y节点列表中的第二个元素。

So y[i] is no longer the source <li> but the instead it's the cloned element and hence y[i].parentNode is the wrapper element. 因此, y[i]不再是源<li>而是源对象<li> ,因此y[i].parentNode是包装器元素。

This means that on the replaceChild line, your code is attempting to make the wrapper element a child of itself. 这意味着在replaceChild行上,您的代码正在尝试使wrapper元素成为其自身的子级。 This is impossible, hence HIERARCHY_REQUEST_ERR 这是不可能的,因此是HIERARCHY_REQUEST_ERR

You are building an invalid DOM. 您正在构建无效的DOM。 <li> must belong in <ul> or <ol> but you're trying to place it in a <span> . <li>必须属于<ul><ol>但是您试图将其放在<span> Hence the error: "A Node was inserted somewhere it doesn't belong" . 因此出现错误: “节点插入到它不属于的位置”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM