Javascript:来自两个 Div ID 的元素

发布于 2024-12-04 09:13:00 字数 704 浏览 3 评论 0原文

我需要选择两个不同导航面板中的所有锚元素。最好的方法是什么?有效和/或高效。

选项 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 技术交流群。

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

发布评论

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

评论(3

落花浅忆 2024-12-11 09:13:00

你已经很接近了:

var allnav = []; // initialise allnav as an array

// Store length of NodeList, slight performance boost in some browsers
// and is just neater
for (var i=0, iLen=subnav.length; i<iLen; i++) { 
   allnav.push(subnav[i]);

   // or

   allnav[i] = subnav[i];
}

不要使用 Array.prototype.slice.call() ,因为你不应该像本地 ECMAScript 对象一样对待主机对象,例如它会在 IE 中失败。 9. 没有任何规范规定主机对象的行为必须类似于本机 ECMA-262 对象(ECMA-262 明确表示它们不必这样做)。

请注意,还有一个 document.anchors 集合和 document.links 集合(其中不是相互排斥的,A 元素可以是锚、链接或两者)。

You are close:

var allnav = []; // initialise allnav as an array

// Store length of NodeList, slight performance boost in some browsers
// and is just neater
for (var i=0, iLen=subnav.length; i<iLen; i++) { 
   allnav.push(subnav[i]);

   // or

   allnav[i] = subnav[i];
}

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).

计㈡愣 2024-12-11 09:13:00
var slice = document.body instanceof Object ? // IE < 9 has some really weird issues
    Array.prototype.slice : function () {
        for (var array = [], i = 0, l = this.length; i < l; ++i)
            array[i] = this[i];
    };

var nav = slice.call(document.getElementById("nav").getElementsByTagName("a"));
var subnav = slice.call(document.getElementById("subnav").getElementsByTagName("a"));

var allnav = nav.concat(subnav); // Will work now.

getElementsByTagName 不返回数组,它返回一个 NodeList,它没有 concat 方法。 slice.call 东西将 NodeList 转换为真正的数组。或者,这样做更有效

nav.push.apply(nav, subnav);
// nav is now the equivalent of allnav
var slice = document.body instanceof Object ? // IE < 9 has some really weird issues
    Array.prototype.slice : function () {
        for (var array = [], i = 0, l = this.length; i < l; ++i)
            array[i] = this[i];
    };

var nav = slice.call(document.getElementById("nav").getElementsByTagName("a"));
var subnav = slice.call(document.getElementById("subnav").getElementsByTagName("a"));

var allnav = nav.concat(subnav); // Will work now.

getElementsByTagName doesn't return an Array, it returns a NodeList, which doesn't have a concat method. The slice.call stuff converts the NodeList into a real Array. Alternatively, it is more efficient to do

nav.push.apply(nav, subnav);
// nav is now the equivalent of allnav
在巴黎塔顶看东京樱花 2024-12-11 09:13:00

getElementsByTagName 返回一个 NodeListNodeList 不是 Array

然而 NodeList 类似于数组,可以使用 toArray 转换为数组

var toArray = function _toArray(obj) {
     var arr = [];
     for (var i = 0, ii = obj.length; i < ii; i++) {
          arr.push(obj[i]);
     }
     return arr;
};

var allnav = toArray(nav).concat(subnav);

或者您可以使用 Array.prototype.slice.call(nav) 来将 NodeList 转换为 Array

您还可以向两个导航添加一个 anchor-nav 类,然后使用 querySelectorAll

var anchors = document.querySelectorAll("nav.anchor-nav a");

getElementsByTagName returns a NodeList. A NodeList is not an Array.

NodeList however is array like and can be converted to an array using toArray

var toArray = function _toArray(obj) {
     var arr = [];
     for (var i = 0, ii = obj.length; i < ii; i++) {
          arr.push(obj[i]);
     }
     return arr;
};

var allnav = toArray(nav).concat(subnav);

Alternatively you can use Array.prototype.slice.call(nav) to convert a NodeList to an Array.

You could also add a class anchor-nav to both nav's and then use querySelectorAll.

var anchors = document.querySelectorAll("nav.anchor-nav a");

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