javascript document.getElementsByClassName() 不像 getElementById 那样工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13784550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
document.getElementsByClassName() not working like getElementById does?
提问by Dylan Cross
Possible Duplicate:
Can getElementsByClassName change style?
I am trying to "fix" the "new" YouTube using Greasemonkey, I've been getting things to work as they should when it's with ID's, however I have been unable to modify anything with a class, it appears I am unable to select it or whatever anyways.
我正在尝试使用 Greasemonkey 来“修复”“新”YouTube,当它带有 ID 时,我一直在按照它们应该的方式工作,但是我一直无法使用类修改任何内容,看来我无法选择它或无论如何。
I have been trying to use:
我一直在尝试使用:
document.getElementsByClassName("video-extras-sparkbar-likes").style.height='10px';
However it isn't working. :\
但是它不起作用。:\
I'm not very familiar with Greasemonkey and what not, so would there be a reason why though my browser supports this, Greasemonkey may not?
我对 Greasemonkey 不是很熟悉,还有什么不熟悉的,那么尽管我的浏览器支持这个,Greasemonkey 可能不支持,这有什么原因吗?
回答by pala?н
You might need to loop through the results, like this:
您可能需要遍历结果,如下所示:
var divs = document.getElementsByClassName('video-extras-sparkbar-likes');
for(var i=0; i < divs.length; i++) {
divs[i].style.height = '10px';
}