我最近一直在尝试了解如何使用 html 树生成器和 JavaScript 选择元素并操作它们。如何选择最后一个子元素的倒数第二个元素以及如何选择链接以操作它们。
要选择我尝试使用的最后一个元素子元素中的倒数第二个元素:
document.firstElementChild.lastElementChild.lastElementChild.lastElementChild
.....但我仍然得到一个空值,因此无法更改元素值。
选择最后一个子元素的倒数第二个元素:
const lastElementChild = document.body.lastElementChild;
const children = lastElementChild.children;
const secondLastElement = children[children.length - 2];
选择和操作链接
const allLinks = document.querySelectorAll('a');
allLinks.forEach(link => {
link.textContent = "";
link.href = "";
});