javascript - Javascript:使用 querySelectorAll() 重新分配数组值更新失败
问题描述
path = element[i].nextElementSibling;
获取更element[i].nextElementSibling
改变量值的path
值,但是当我重新分配的值element[i]
element[i] = path;
保持element[i]
原样。为什么会这样?
/*
Press Ctrl+Shift+C while opening the web console with google chrome or mozzila firefox (Tools > Web Developer > Web Console) and hover over the element display by the console to highlight the element.
*/
let element = document.querySelectorAll("a");
console.log(element.length);
for (let i = 0; i < element.length; i++) {
let path = element[i].nextElementSibling;
//Is NOT next sibling element exist and is <p>
if(!(path && path.matches("p"))){
//path is null, reasign to <a>
path = element[i];
console.log("path = element[i] (Origin):");
console.log(path = element[i]);
//get <a> parent element
path = path.parentElement;
console.log("path = path.parentElement (Origin's Parent):");
console.log(path); //display <div>
if(path && path.nextElementSibling.matches("p")){
//get destination
element[i] = path.nextElementSibling;
//fail to changed T_T
console.log("element[i] = path.nextElementSibling; (Origin's Parent's Next Sibling)");
console.log("'path.nextElementSibling' value:");
console.log(path.nextElementSibling); //display <p>
console.log("Value of 'element[i]' (did not change!):");
console.log(element[i]); //why still <a>? not <p>
}
}else{ //sibling element
element[i] = path;
console.log("element[i] = path");
console.log("'path' value:");
console.log(path);
console.log("Value of 'element[i]' (did not change!):");
console.log(element[i]);
}
console.log("FINAL value of element[i]:");
element[i].classList.add("get--target");
console.log(element[i]);
}
body{
font-size: 20px;
font-weight: bold;
color: white;
background: black;
}
div{
background: navy;
padding: 20px;
}
span{
display: none;
margin: 10px 0;
padding: 10px;
}
a, .get--target{
display: block;
margin: 10px;
padding: 10px;
}
a{
text-decoration: none;
color: white;
background: orange;
}
p{
background: blue;
padding: 10px;
}
p.get--target span{
display: block;
background: lightgreen;
}
a.get--target span{
display: block;
background: red;
}
<body>
<div>
<a href="#">Origin <span>Failed</span></a>
<p>Destination <span>Success</span></p>
</div>
<p>Destination <span>Success</span></p>
</body>
PS Google Chrome 和 Mozilla Firefox 有不同的控制台日志显示第一个结果element[i]
还没有设置有get--target
类,在 Google Chrome 中有吗?
Google Chrome 和 Mozilla Firefox 控制台日志结果
解决方案
我认为原因是 document.querySelectorAll() 方法将 NodeList 对象。无法修改 NodeList 对象。为了更改 NodeList 对象,您必须将其转换为普通的 javascript 数组。Array.from(document.querySelectorAll('[selector]')) 将返回 javascript 数组而不是 NodeList Object。希望这会有所帮助。
/*
Press Ctrl+Shift+C while opening the web console with google chrome or mozzila firefox (Tools > Web Developer > Web Console) and hover over the element display by the console to highlight the element.
*/
let element = Array.from(document.querySelectorAll("a"));
console.log(element.length);
for (let i = 0; i < element.length; i++) {
let path = element[i].nextElementSibling;
//Is NOT next sibling element exist and is <p>
if(!(path && path.matches("p"))){
//path is null, reasign to <a>
path = element[i];
console.log("path = element[i] (Origin):");
console.log(path = element[i]);
//get <a> parent element
path = path.parentElement;
console.log("path = path.parentElement (Origin's Parent):");
console.log(path); //display <div>
if(path && path.nextElementSibling.matches("p")){
//get destination
element[i] = path.nextElementSibling;
//fail to changed T_T
console.log("element[i] = path.nextElementSibling; (Origin's Parent's Next Sibling)");
console.log("'path.nextElementSibling' value:");
console.log(path.nextElementSibling); //display <p>
console.log("Value of 'element[i]' (did not change!):");
console.log(element[i]); //why still <a>? not <p>
}
}else{ //sibling element
element[i] = path;
console.log("element[i] = path");
console.log("'path' value:");
console.log(path);
console.log("Value of 'element[i]' (did not change!):");
console.log(element[i]);
}
console.log("FINAL value of element[i]:");
element[i].classList.add("get--target");
console.log(element[i]);
}
body{
font-size: 20px;
font-weight: bold;
color: white;
background: black;
}
div{
background: navy;
padding: 20px;
}
span{
display: none;
margin: 10px 0;
padding: 10px;
}
a, .get--target{
display: block;
margin: 10px;
padding: 10px;
}
a{
text-decoration: none;
color: white;
background: orange;
}
p{
background: blue;
padding: 10px;
}
p.get--target span{
display: block;
background: lightgreen;
}
a.get--target span{
display: block;
background: red;
}
<body>
<div>
<a href="#">Origin <span>Failed</span></a>
<p>Destination <span>Success</span></p>
</div>
<p>Destination <span>Success</span></p>
</body>
推荐阅读
- amazon-web-services - Lambda 上的 AWS SES - 无法(静默)发送电子邮件
- python - Tkinter 标签在时间睡眠后没有出现
- google-drive-api - 如何恢复永久删除的谷歌驱动器文件
- javascript - 如何从导航选项中的文本输入更新本机组件的状态?
- recursion - c中的递归函数有问题
- vba - 循环中的 VBA 变量 - BlueZone 仿真
- terraform - 未使用 Terraform 和 vSphere 重新创建的资源
- mysql - 如何使用另一个查询结果过滤 MySQL 表
- c - 我无法与 gcc-7 链接
- python - 使用共享基本名称的 Python 包