如何通过类名获取子元素? - How to get child element by class name?

问题:

I'm trying to get the child span that has a class = 4. Here is an example element:我正在尝试获取具有 class = 4 的子跨度。这是一个示例元素:

<div id="test">
 <span class="one"></span>
 <span class="two"></span>
 <span class="three"></span>
 <span class="four"></span>
</div>

The tools I have available are JS and YUI2.我可用的工具是 JS 和 YUI2。 I can do something like this:我可以做这样的事情:

doc = document.getElementById('test');
notes = doc.getElementsByClassName('four');

//or

doc = YAHOO.util.Dom.get('#test');
notes = doc.getElementsByClassName('four');

These do not work in IE.这些在 IE 中不起作用。 I get an error that the object (doc) doesn't support this method or property (getElementsByClassName).我收到一个错误,指出对象 (doc) 不支持此方法或属性 (getElementsByClassName)。 I've tried a few examples of cross browser implementations of getElementsByClassName but I could not get them to work and still got that error.我已经尝试了几个 getElementsByClassName 跨浏览器实现的例子,但我无法让它们工作并且仍然得到那个错误。

I think what I need is a cross browser getElementsByClassName or I need to use doc.getElementsByTagName('span') and loop through until I find class 4. I'm not sure how to do that though.我想我需要的是跨浏览器 getElementsByClassName 或者我需要使用 doc.getElementsByTagName('span') 并循环直到找到第 4 类。不过我不知道该怎么做。


解决方案:

参考一: https://en.stackoom.com/question/p37x
参考二: https://stackoom.com/question/p37x
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章