<div class="t">
<span class="y" id="A1">A</span>
<span class="y" id="B1">B</span>
<span class="y" id="C1">C</span>
...
</div>如何获取A1 B1 C1 ... ?
3 回答
已采纳
weibo_哆啦A梦有大口袋_0
TA贡献107条经验 获得超146个赞
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试</title> </head> <body> <div class="t"> <span id="A1" class="red">A</span> <span id="B1" class="green">B</span> <span id="C1" class="blue">C</span> </div> </body> </html> <script type="text/javascript"> //DOM节点加载完成后执行 window.onload=function(){ var A=document.getElementById('A1'); var B=document.getElementById('B1'); var C=document.getElementById('C1'); //测试 var Aclass=A.getAttribute("class"); var Bid=B.getAttribute("id"); var Cclass=C.getAttribute("class"); alert(Aclass+Bid+Cclass); } </script>
出于对回答者的认同和尊重,解决了你的问题,请采纳,谢谢
柠檬酸钠
TA贡献331条经验 获得超534个赞
var spans = document.getElementsByClassName('t')[0].getElementsByTagName('span'); for (var i=0;i<spans.length;i++) { console.log(spans[i].getAttribute('id')); }
黑色旋风
TA贡献20条经验 获得超25个赞
<script> var t = document.getElementById('A1'); document.write(t.getAttribute('id')); </script>
呐,getAttribute()。
添加回答
举报
0/150
提交
取消