当我单击h1 tag
之一时,无法更改p tags
。我在遍历p tags
,提取值并用该数据值更改标题时遇到麻烦。到目前为止,我只收到错误“无法读取value
的属性undefined
。
window.addEventListener('load', function() {
'use strict';
const p_array = document.getElementsByTagName("p");
const count = p_array.length;
//loop through a list of elements.
for (let i = 0; i < count; i++) {
const p = p_array[i];
p.addEventListener("click", function() {
const tag = p.getElementsByTagName('p');
document.getElementById("title").innerHTML = tag.dataset.value;
})
}
});
<h1 id="title">Title</h1>
<p data-value="One">Click to change title to One</p>
<p data-value="Two">Click to change title to Two</p>
<p data-value="Three">Click to change title to Three</p>
答案 0 :(得分:1)
getElementsByTagName
返回节点的集合。如果您对当前的p
感兴趣,只需使用this
或事件目标:
p.addEventListener("click", function (e) {
const tag = e.target
// or const tag = this
document.getElementById("title").innerHTML = tag.dataset.value;
})
答案 1 :(得分:0)
window.addEventListener('load', function() {
'use strict';
const p_array = document.getElementsByTagName("p");
const count = p_array.length;
//loop through a list of elements.
for (let i = 0; i < count; i++) {
const p = p_array[i];
p.addEventListener("click", function() {
// Here the `this` keyword refers to the element that is clicked
document.getElementById("title").innerHTML = this.dataset.value;
})
}
});
<h1 id="title">Title</h1>
<p data-value="One" data-doo="foo">Click to change title to One</p>
<p data-value="Two">Click to change title to Two</p>
<p data-value="Three">Click to change title to Three</p>
答案 2 :(得分:0)
第1步:
使用Array.from()从getElementsByTagName()
返回的HTMLCollection中构建一个数组。
第2步:
在每个元素上设置一个事件侦听器,并使用单击的元素data-value
来更改#title
window.addEventListener('load', function() {
'use strict';
Array.from(document.getElementsByTagName("p")).forEach(ele => {
ele.addEventListener("click", function() {
document.getElementById("title").innerHTML = ele.dataset.value;
})
});
});
<h1 id="title">Title</h1>
<p data-value="One">Click to change title to One</p>
<p data-value="Two">Click to change title to Two</p>
<p data-value="Three">Click to change title to Three</p>
答案 3 :(得分:0)
window.addEventListener('load', function() {
'use strict';
const p_array = document.getElementsByTagName("p");
const count = p_array.length;
//loop through a list of elements.
for (let i = 0; i < count; i++) {
const p = p_array[i];
p.addEventListener("click", function() {
//const tag = p.getElementsByTagName('p');
document.getElementById("title").innerHTML = p.dataset.value;
})
}
});
<h1 id="title">Title</h1>
<p data-value="One">Click to change title to One</p>
<p data-value="Two">Click to change title to Two</p>
<p data-value="Three">Click to change title to Three</p>
答案 4 :(得分:0)
这可以解决问题(仅适用于javascript) p.getAttribute('data-value');返回指定属性的值
window.addEventListener('load', function() {
'use strict';
const p_array = document.getElementsByTagName("p");
const count = p_array.length;
//loop through a list of elements.
for (let i = 0; i < count; i++) {
const p = p_array[i];
p.addEventListener("click", function(){
alert(p.getAttribute('data-value'));
document.getElementById("title").innerHTML = p.getAttribute('data-value');
});
}
});