DOM 脚本 getElementsByClassName(用于链接)
我正在练习 DOM 脚本(不是现实生活中的问题,而是实践和理论,我当然知道如何使用 jQuery 实现这一点) 因此,我正在尝试改进和理解以下内容:
我有一些与类的链接,并且我在它们上附加了事件:
<a href="http://www.google.com" class="popup">click</a>
<a href="http://www.test.com" class="popup">click2</a>
<a href="http://www.abc.com" class="popup">click3</a>
<a href="http://www.google.com" class="no">click4</a>
Javascript:
window.onload = prepareLinks;
function prepareLinks() {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links[i].getAttribute("class") == "popup") {
links[i].onclick = function () {
popUp(this.getAttribute("href"));
return false;
}
}
}
}
function popUp(winURL) {
window.open(winURL, "popup", "width=320,height=480");
}
效果很好。我基本上是从书上得到的。现在我想通过使用 getElementsByClassName 来改进它。我继续写:
window.onload = prepareLinks;
function prepareLinks() {
var links = document.getElementsByTagName("a");
var popups = links.getElementsByClassName("popup");
for (var i = 0; i < popups.length; i++) {
popups[i].onclick = function () {
popUp(this.getAttribute("href"));
return false;
}
}
}
function popUp(winURL) {
window.open(winURL, "popup", "width=320,height=480");
}
但我得到了错误: Uncaught TypeError: Object # has no method 'getElementsByClassName'
显然 links 是一个 NodeList 所以我不能在它上面使用 getElementsByClassName 。我真的不太明白... 您能帮助我了解如何做到这一点以及该脚本的第一个版本是否良好? (性能方面)。谢谢。
I am practicing DOM Scripting (no real-life problem but rather practice and theory, I know how to achieve this with jQuery of course)
So, I am trying to improve and understand the following:
I have some links with classes and I am attaching event on them:
<a href="http://www.google.com" class="popup">click</a>
<a href="http://www.test.com" class="popup">click2</a>
<a href="http://www.abc.com" class="popup">click3</a>
<a href="http://www.google.com" class="no">click4</a>
Javascript:
window.onload = prepareLinks;
function prepareLinks() {
var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
if (links[i].getAttribute("class") == "popup") {
links[i].onclick = function () {
popUp(this.getAttribute("href"));
return false;
}
}
}
}
function popUp(winURL) {
window.open(winURL, "popup", "width=320,height=480");
}
That works fine. I got it from a book basically. Now I want to improve it by making use of getElementsByClassName. I went on to write:
window.onload = prepareLinks;
function prepareLinks() {
var links = document.getElementsByTagName("a");
var popups = links.getElementsByClassName("popup");
for (var i = 0; i < popups.length; i++) {
popups[i].onclick = function () {
popUp(this.getAttribute("href"));
return false;
}
}
}
function popUp(winURL) {
window.open(winURL, "popup", "width=320,height=480");
}
But I got error:
Uncaught TypeError: Object # has no method 'getElementsByClassName'
Apparently links is a NodeList so I can't use the getElementsByClassName on it. Which I don't really understand...
Can you help on how I could do this, and whether or not the first version of the script is good? (performance wise). Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用 getElement* 函数这样相互过滤,因为它们不对列表进行操作,并且无论如何也不在结果中返回原始节点。如果您需要同时过滤多个条件,请使用
querySelectorAll
,例如document.querySelectorAll("a.popup")
。You can't use getElement* functions to filter each other like that, because they don't operate on lists, and they don't return the original node in their results anyway. If you need to filter on more than one condition simultaneously, use
querySelectorAll
instead, e.g.document.querySelectorAll("a.popup")
.第一个版本很好,但如果您首先按类名获取元素,然后按标签名称过滤它们,如果实际上您甚至需要按标签名称过滤它们,您可能会看到改进。如果
popup
类仅用于链接,则不需要getElementsByTagName
,当然,如果删除它,您的脚本将会加速。The first version is fine, but you might see an improvement if you got the elements by class name first, then filtered them by tag name, if in fact you do even need them filtered by tag name. If the class
popup
will only be used on links,getElementsByTagName
is unnecessary and your script will speed up if you remove it, of course.