admin管理员组文章数量:1023758
I need write a function called that takes in one argument, which is a list of elements. In the list of elements there's ONE element that has an ID,but I don't know what's the ID called. The function should change the inner text of the element to be the value of the ID. Example:
<div><div/>
<div><div/>
<div><div/>
<div id="special"><div/>
<div><div/>
let divs = querySelectorAll('div')
findId(divs)
What it should do :
<div><div/>
<div><div/>
<div><div/>
<div id="special">special<div/>
<div><div/>
I have tried using innerhtml but I do not know how to go about doing it,as the id is unknown.
Please help out.
I need write a function called that takes in one argument, which is a list of elements. In the list of elements there's ONE element that has an ID,but I don't know what's the ID called. The function should change the inner text of the element to be the value of the ID. Example:
<div><div/>
<div><div/>
<div><div/>
<div id="special"><div/>
<div><div/>
let divs = querySelectorAll('div')
findId(divs)
What it should do :
<div><div/>
<div><div/>
<div><div/>
<div id="special">special<div/>
<div><div/>
I have tried using innerhtml but I do not know how to go about doing it,as the id is unknown.
Please help out.
5 Answers
Reset to default 2Something like this;
function change(divs){
divs.forEach((div) => {
if(div.id){
div.innerHTML = div.id;
}
})
}
let divs = document.querySelectorAll('div');
change(divs);
<div></div>
<div id="special"></div>
<div></div>
<div></div>
To determine if the element has an ID, you can just use the hasAttribute()
method:
// Array.prototype.slice.call() is an easy way to turn the NodeList document.querySelector returns into an array.
const divs = Array.prototype.slice.call(document.querySelectorAll('div'));
const haveIds = divs.filter(element =>
element.hasAttribute('id')
);
console.log(haveIds);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
Alternatively, you can also use querySelector
itself to determine it as well:
const withIds = document.querySelectorAll('div[id]');
console.log(withIds);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
That [id]
part basically means "if it has the attribute ID".
Regardless which method you choose, then you just loop through and set the innerText to their ID:
const withIds = Array.prototype.slice.call(document.querySelectorAll('div[id]'));
withIds.forEach(element => element.innerText = element.id);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
modern syntax:
let divs = document.body.querySelectorAll("div");
for (const div of divs) {
if (div.hasAttribute("id")) {
const id = div.id;
div.innerHTML = id;
}
}
Iterate through list of elements and check them by css
selector: .matches('div[id]')
. Use textContent
of found one.
let divs = document.querySelectorAll('div');
let result = findId(divs);
console.log(result);
function findId(list) {
for (var i = 0; i < list.length; i++) {
if (list[i].matches('div[id]')) {
return list[i].textContent;
}
}
return null;
}
const theDivs = document.querySelectorAll('div');
if (theDivs) {
theDivs.forEach(function(el) {
if (el.id) {
el.innerHTML = el.id;
}
});
} else {
alert('Sorry, no divs found buddy');
}
div {
padding: 1rem;
border: lightgray 1px dotted;
width: 10rem;
}
<div></div>
<div></div>
<div id="special"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
I need write a function called that takes in one argument, which is a list of elements. In the list of elements there's ONE element that has an ID,but I don't know what's the ID called. The function should change the inner text of the element to be the value of the ID. Example:
<div><div/>
<div><div/>
<div><div/>
<div id="special"><div/>
<div><div/>
let divs = querySelectorAll('div')
findId(divs)
What it should do :
<div><div/>
<div><div/>
<div><div/>
<div id="special">special<div/>
<div><div/>
I have tried using innerhtml but I do not know how to go about doing it,as the id is unknown.
Please help out.
I need write a function called that takes in one argument, which is a list of elements. In the list of elements there's ONE element that has an ID,but I don't know what's the ID called. The function should change the inner text of the element to be the value of the ID. Example:
<div><div/>
<div><div/>
<div><div/>
<div id="special"><div/>
<div><div/>
let divs = querySelectorAll('div')
findId(divs)
What it should do :
<div><div/>
<div><div/>
<div><div/>
<div id="special">special<div/>
<div><div/>
I have tried using innerhtml but I do not know how to go about doing it,as the id is unknown.
Please help out.
5 Answers
Reset to default 2Something like this;
function change(divs){
divs.forEach((div) => {
if(div.id){
div.innerHTML = div.id;
}
})
}
let divs = document.querySelectorAll('div');
change(divs);
<div></div>
<div id="special"></div>
<div></div>
<div></div>
To determine if the element has an ID, you can just use the hasAttribute()
method:
// Array.prototype.slice.call() is an easy way to turn the NodeList document.querySelector returns into an array.
const divs = Array.prototype.slice.call(document.querySelectorAll('div'));
const haveIds = divs.filter(element =>
element.hasAttribute('id')
);
console.log(haveIds);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
Alternatively, you can also use querySelector
itself to determine it as well:
const withIds = document.querySelectorAll('div[id]');
console.log(withIds);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
That [id]
part basically means "if it has the attribute ID".
Regardless which method you choose, then you just loop through and set the innerText to their ID:
const withIds = Array.prototype.slice.call(document.querySelectorAll('div[id]'));
withIds.forEach(element => element.innerText = element.id);
<div>A</div>
<div>B</div>
<div id="me">C</div>
<div>D</div>
<div id="me-too">E</div>
modern syntax:
let divs = document.body.querySelectorAll("div");
for (const div of divs) {
if (div.hasAttribute("id")) {
const id = div.id;
div.innerHTML = id;
}
}
Iterate through list of elements and check them by css
selector: .matches('div[id]')
. Use textContent
of found one.
let divs = document.querySelectorAll('div');
let result = findId(divs);
console.log(result);
function findId(list) {
for (var i = 0; i < list.length; i++) {
if (list[i].matches('div[id]')) {
return list[i].textContent;
}
}
return null;
}
const theDivs = document.querySelectorAll('div');
if (theDivs) {
theDivs.forEach(function(el) {
if (el.id) {
el.innerHTML = el.id;
}
});
} else {
alert('Sorry, no divs found buddy');
}
div {
padding: 1rem;
border: lightgray 1px dotted;
width: 10rem;
}
<div></div>
<div></div>
<div id="special"></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
本文标签: javascripthow Change the text of the inner html to that of the idStack Overflow
版权声明:本文标题:javascript - how Change the text of the inner html to that of the id - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745516865a2154108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论