javascript getElementsByClassName()总是返回none? [英] javascript getElementsByClassName() always returns none?
本文介绍了javascript getElementsByClassName()总是返回none?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,
i想为我的浏览器创建最简单的书签。
hey guys, i wanna create the simplest bookmarklet for my browser.
javascript:document.getElementsByClassName('source').style.visibility='visible';
我的身体里有多个div.source。默认情况下,它们设置为 .source {display:none; }
with css。
I have multiple div.source in my body. By default they are set to .source { display:none; }
with css.
我的控制台告诉我:未捕获的TypeError:无法设置未定义的属性'display'
当我点击书签时,所有.source div都应该是可见的。我在这里做错了什么?
When I click the bookmarklet all .source divs should be visible. What am I doing wrong here?
推荐答案
您可能需要遍历结果,如下所示:
You might need to loop through the results, like this:
var divs = document.getElementsByClassName('source');
for(var i=0; i<divs.length; i++) {
divs[i].style.display='block'
}
同样如@ionoy所述,使用 display
属性。我希望有所帮助。
And also as @ionoy mentioned, use display
attribute. I hope that helps.
http:// jsfiddle.net/erick/rb7bn/1/
这篇关于javascript getElementsByClassName()总是返回none?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文