我想知道如何成功地将 HTMLCollection 包装到 jQuery/Zepto 函数中。
我有以下相同的选择器:一个在 Zepto 中,另一个在包含 Zepto 函数的普通 JavaScript 中。不过,后者并不能让 Zepto 方法对其节点可用。
$('#navigation a');
$( document.getElementById('navigation').getElementsByTagName('a') );
我想让它们相等。
编辑:我使用 Zepto 而不是 jQuery,并且没有提及它,假设 Zepto 的行为方式与 jQuery 相同。
最佳答案
您可以尝试以下方法(未经测试)
var collection = document.getElementById('navigation').getElementsByTagName('a');
$().add(Array.prototype.slice.call(collection));
根据 add
的文档(请参阅 docu ),应该可以将多个元素添加到当前的 jQuery 对象。
$( ... ).add( elements )
One or more elements to add to the set of matched elements.
关于javascript - 将 HTMLCollection 包装到 jQuery/Zepto 函数中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23876173/