我的代码:
Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
el.textContent = "Test";
});
这将按价值传递,不会根据我的阅读进行分配。
Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el =>
{
arr[index].textContent = "test";
});
应该是对每个元素的引用并且应该有效。但事实并非如此。是因为我创建的数组是未命名的,因此我无法轻易识别它?
我的问题 - 为什么第二版不起作用?它不是在我的chrome脚本上进行分配,即使我知道它正在被执行。我所在的页面没有显示任何更改,但是以MTG开头的每个元素都应该将其文本内容更改为“test”。
不是。为什么呢?
编辑:
像这样:
Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") )[0].textContent
评估文本,所以我知道语法至少是可靠的。
答案 0 :(得分:1)
这应该可以正常工作
Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( el => {
el.textContent = "Test";
});
第二个应该是
Array.from(document.querySelectorAll("[id^='MTG_INSTR$']") ).forEach( (el,index,arr) =>
{
arr[index].textContent = "test";
});