如何通過類名獲取子元素? - 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章