Javascript - 将多个节点列表连接在一起
我最初要求一种优雅的方式来模拟 IE 或旧版浏览器中的 getElementsByTagName 函数结果的 Array.concat() 功能,因为看起来不支持 >concat。只是,当然是这样——返回的对象不支持它的原因是因为它不是Array
。哎呀!
getElementsByTagName
实际上返回一个 NodeList
。那么,真正的问题是:获取文档中所有表单元素(输入、选择、文本区域、按钮)的单个列表以循环遍历它们的好方法是什么?不需要数组...单个 NodeList
也将是完美的。
请注意,我使用的是 IE6,因为这是用于公司内部网(不过很快就会使用 IE8)。
我想出的答案是:
将代码放入一个单独的函数中并使用不同的节点列表调用它三次,而不用担心如何将它们塞到一起,这会变得更简单,并且性能可能会更好一个。
我最终转而使用 MooTools(经过几个小时阅读所有不同框架的比较)。所以现在,获取我想要的项目数组非常简单。我建议使用像这样的 javascript 框架,而不是人们绞尽脑汁试图找出做事的最佳方法。当然,我完全赞成实际学习原始语言(这就是为什么我这么长时间没有使用框架的原因),但这并不总是让事情进展最快的方法,这在企业中通常同样重要提高编码人员使用该语言的能力。
更新:差不多 2 年后,我只会使用 jQuery 并完成它!
I was originally asking for an elegant way to simulate the Array.concat()
functionality on the results of the getElementsByTagName
function in IE or older browsers, because it seemed that concat
was not supported. Only, of course it is--the reason the returned object didn't support it is because it isn't an Array
. Oops!
getElementsByTagName
actually returns a NodeList
. The real question, then, is: what's a good way to get a single list of all the form elements in a document (input, select, textarea, button) to loop through them? An array isn't required... a single NodeList
would be perfect, too.
Note that I'm using IE6 as this is for a corporate intranet (soon IE8 though).
The answer that I came up with was:
It became simpler and probably performed better to just put the code into a separate function and call it three times with the different nodelists, rather than worry about a good way to cram them together into one.
I ultimately switched to using MooTools (after several hours reading up on comparisons of all the different frameworks). So now, getting an array of the items I want is very simple. I recommend using a javascript framework like this rather than people beating their brains out trying to figure out the best way to do things. Of course I'm all for actually learning the raw language (which is why I've held off using a framework for so long) but it isn't always the fastest way to get things going, which in a business often matters as much as improving the coder's ability with the language.
Update: almost 2 years later I would just use jQuery and be done with it!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我本以为会有比这更多的答案,无论如何,我尝试了一下并提出了以下函数,尽管我不得不稍微敲一下头。
I'd of thought there would have been more answers than this, anyway I gave this a shot and came up with the following function although I had to bang my head a little.
要连接节点列表,请使用 Array.prototype.slice.call 将它们转换为数组,然后正常连接它们。
编辑:(回应您的评论)
如果您只有几种类型的元素,这没关系,但性能会随着您进行的 DOM 调用次数而降低。执行
document.getElementsByTagName('*')
、循环遍历列表并选择具有所需nodeName
的元素可能会更好更快。另一件需要记住的事情是上面使用的 Array.prototype.slice 方法可能不适用于所有浏览器。查看 sizzle.js 中的注释起始行#723(选择器jQuery 背后的引擎)
当然,最好使用像 jQuery 这样的库来处理所有令人头疼的问题。您可以简单地执行以下操作:
To concatenate nodelists, convert them into arrays using
Array.prototype.slice.call
and then concat them normally.Edit: (Responding to your comment)
If you only have a few types of elements, this is okay but the performance decreases with the number of DOM calls you make. It may be better and faster to do a
document.getElementsByTagName('*')
, loop thru the list and pick the elements with the requirednodeName
.Another thing to keep in mind is that the
Array.prototype.slice
method used above may not work in ALL browsers. Check out the comment starting line#723 in sizzle.js (the selector engine behind jQuery)Of course, it is best to use a library like jQuery which handles all the headache. You can simply do:
使用 ES6 扩展运算符,您可以执行以下操作:
With the ES6 spread operator you can do
console.log( mergeNodeLists( 输入, 选择 ) ); // => [输入、选择]
console.log( mergeNodeLists( inputs, selects ) ); // => [input, select]
正如 MDN 文档 中所述,您还可以使用 < a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from" rel="nofollow noreferrer">
Array.from
< /a> 在支持它的浏览器上将 NodeList 转换为数组。As noted in the MDN Documentation, you can also use
Array.from
to convert a NodeList in to an Array on browsers that support it.