以下是HTML代码:
<ul id="current">
<li class="session">
<dl>
<dt>Mumbai, Maharashtra, India</dt>
<dd>Approximate location</dd>
<dt>Safari on Mac OS X</dt>
<dt>IP Address:</dt>
<dd>110.227.252.240</dd>
</dl>
</li>
我想要所有dt
和dd
元素
答案 0 :(得分:2)
使用document.querySelectorAll
获取所有元素
var x = document.querySelectorAll("dt");
var y = document.querySelectorAll("dd");
如果要更改其任何属性,请使用for循环
var i;
for (i = 0; i < x.length; i++) {
x[i].style.color = "red";
}
答案 1 :(得分:0)
使用querySelectorAll()
:
let dt = document.querySelectorAll('#current > .session > dl > dt');
let dd = document.querySelectorAll('#current > .session > dl > dd');
dt.forEach(function(el){
el.style.color = 'blue';
});
dd.forEach(function(el){
el.style.color = 'red';
});
&#13;
<ul id="current">
<li class="session">
<dl>
<dt>Mumbai, Maharashtra, India</dt>
<dd>Approximate location</dd>
<dt>Safari on Mac OS X</dt>
<dt>IP Address:</dt>
<dd>110.227.252.240</dd>
</dl>
</li>
</ul>
&#13;