gpt4 book ai didi

javascript - getElementsByClassName 闭包问题

转载 作者:行者123 更新时间:2023-12-02 16:44:29 26 4
gpt4 key购买 nike

似乎每次都从闭包外部重新执行getElementsByClassName

我可以通过将元素复制到数组中来解决这个问题,但我不知道是否有更好的方法。

这是一个代码示例:

HTML

<body>
<ul>
<li class="tab">Parent Tab 1</li>
<li class="tab">Parent Tab 2</li>
</ul>
<div class="tab-content">Parent Content 1</div>
<div class="tab-content">Parent Content 2</div>

<!-- dynamically generated by javascript -->
<div id="unrelatedContent">
<ul>
<li class="tab">Child Tab 1</li>
<li class="tab">Child Tab 2</li>
</ul>
<div class="tab-content">Child Content 1</div>
<div class="tab-content">Child Content 2</div>
</div>
</body>

JavaScript

var tabs = function(parent) {
var tabs = parent.getElementsByClassName("tab");
var tabContents = parent.getElementsByClassName("tab-content");
var tabContentsById = {};
var t1 = [];
var t2 = [];

//copy to an array. it seems like chrome re-evaluates
//the getElementsByClassName part on click, meaning you cant
//have nested tabs otherwise the parent will affect the children
//(because itll match the childrens tab and tab-content)
/*
for (var i = 0; i < tabs.length; i++) {
t1.push(tabs[i]);
}
for (var i = 0; i < tabContents.length; i++) {
t2.push(tabContents[i]);
}
tabs = t1;
tabContents = t2;
*/

for (var i = 0; i < tabs.length; i++) {
var tab = tabs[i];
tab.onclick = function() {
console.log(tabs);
}
}
}

tabs(document.getElementsByTagName("body")[0]);

parentTab1.click(); //length = 2

//pretend like unrelatedContent was dynamically generated via dom methods
document.getElementsByTagName("body")[0].appendChild(unrelatedContent);
tabs(unrelatedContent);

parentTab1.click(); //length = 4

最佳答案

document.getElementsByClassName() 的结果是 HTMLCollection这是一个实时的元素集合。如果您不希望在向文档添加更多元素时更新此集合,则需要将副本复制到数组中;这是克隆容器的简单方法:

var tabs = [].slice.call(parent.getElementsByClassName("tab"), 0);
var tabContents = [].slice.call(parent.getElementsByClassName("tab-content"), 0);

另请参阅:Array.prototype.slice

关于javascript - getElementsByClassName 闭包问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com