在下面的 HTML
<nav>
<a href="home.html">home</a>
<a href="about.html">about</a>
<a href="more.html">more</a>
<a href="contact.html">contact</a>
</nav>
应该反转 <a>
.
JavaScript
创建一个反向数组:
var n = document.querySelector("nav");
var max = n.children.length;
var arrReverse = [];
for (i = (max - 1); i > -1; i -= 1) {
arrReverse.push(n.children[i]);
}
我认为这会起作用:
for(j = 0; j < max; j += 1) {
n.children[j].innerHTML = arrReverse[j].innerHTML;
n.children[j].href = arrReverse[j].href;
}
但事实并非如此。输出为 contact more more contact
.
有人可以解释一下,为什么这不起作用。
最佳答案
当你改变on.children's arr Reverse
改变为
var n = document.querySelector("nav");
var max = n.children.length;
var arrReverse = [];
for(i = (max - 1); i > -1; i -= 1) {
arrReverse.push([n.children[i].innerHTML,n.children[i].href]);
}
for(j = 0; j < max; j += 1) {
// console.log(arrReverse[j].innerHTML);
n.children[j].innerHTML = arrReverse[j][0];
n.children[j].href = arrReverse[j][1];
}
关于javascript - 反向HTML标签,理解赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34040861/