Javascript:来自两个 Div ID 的元素
我需要选择两个不同导航面板中的所有锚元素。最好的方法是什么?有效和/或高效。
选项 1:我可以将每个导航设置为一个类,查找该类,获取该类的每个 div 内的所有锚点。
选项 2:我可以为每个导航设置一个 ID。由于某种原因,我无法连接每个 div id 中的两个锚点数组。知道为什么吗,它们是数组吗?前任。
<code>
var nav = document.getElementById("nav").getElementsByTagName("a");
var subnav = document.getElementById("subnav").getElementsByTagName("a");
var allnav = nav.concat(subnav); // Didn't seem to work
// neither did this. Just seemed to break.
for(var i=0;i<subnav.length;i++){
nav.push(subnav[i]);
}
</code>
选项 3:通过 ID 获取每个 div。发送到函数以循环、获取锚点并执行适当的操作。
哪个会更快或使用更少的资源,和/或您认为哪个更易于维护?
我知道 jQuery 有一个很好的方法,但考虑到我的代码片段很短,我不想只为几个小函数添加整个库。
感谢您的帮助!
I need to select all the anchor elements in two different navigation panels. What's the best way to do so? Effective &/or efficient.
Option 1: I can set each nav to a class, look for the class, grab all the anchors within each div with that class.
Option 2: I can set each nav with an ID. For some reason I had trouble concat the two arrays of anchors from each div id. Any idea why, are they arrays? Ex.
<code>
var nav = document.getElementById("nav").getElementsByTagName("a");
var subnav = document.getElementById("subnav").getElementsByTagName("a");
var allnav = nav.concat(subnav); // Didn't seem to work
// neither did this. Just seemed to break.
for(var i=0;i<subnav.length;i++){
nav.push(subnav[i]);
}
</code>
Option 3: Get each div by ID. Send to function to loop through, get anchors, and do appropriate action.
Which would be quicker or use less resources, &/or which do you think would be more maintainable?
I know there's an excellent method with jQuery, but given my snippet is quite short, I'd prefer not to add the entire library for just a few small functions.
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你已经很接近了:
不要使用 Array.prototype.slice.call() ,因为你不应该像本地 ECMAScript 对象一样对待主机对象,例如它会在 IE
中失败。 9. 没有任何规范规定主机对象的行为必须类似于本机 ECMA-262 对象(ECMA-262 明确表示它们不必这样做)。
请注意,还有一个 document.anchors 集合和 document.links 集合(其中不是相互排斥的,A 元素可以是锚、链接或两者)。
You are close:
Don't use
Array.prototype.slice.call()
because you should not treat host objects like native ECMAScript objects, e.g. it will fail in IE < 9. There is no specification anywhere to say that host objects must behave like native ECMA-262 objects (and ECMA-262 expressly says they don't have to).Note that there is also a document.anchors collection and a document.links collection (which are not mutually exclusive, an A element can be an anchor, a link or both).
getElementsByTagName
不返回数组,它返回一个 NodeList,它没有concat
方法。slice.call
东西将 NodeList 转换为真正的数组。或者,这样做更有效getElementsByTagName
doesn't return an Array, it returns a NodeList, which doesn't have aconcat
method. Theslice.call
stuff converts the NodeList into a real Array. Alternatively, it is more efficient to dogetElementsByTagName
返回一个NodeList
。NodeList
不是Array
。然而 NodeList 类似于数组,可以使用 toArray 转换为数组
或者您可以使用 Array.prototype.slice.call(nav) 来将
NodeList
转换为Array
。您还可以向两个导航添加一个
anchor-nav
类,然后使用querySelectorAll
。var anchors = document.querySelectorAll("nav.anchor-nav a");
getElementsByTagName
returns aNodeList
. ANodeList
is not anArray
.NodeList
however is array like and can be converted to an array usingtoArray
Alternatively you can use
Array.prototype.slice.call(nav)
to convert aNodeList
to anArray
.You could also add a class
anchor-nav
to both nav's and then usequerySelectorAll
.var anchors = document.querySelectorAll("nav.anchor-nav a");