最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JS change all 'p' color - Stack Overflow

programmeradmin0浏览0评论

i want to change color all 'p' elements, i tried do it with this code but it not works.

document.getElementsByTagName('p').forEach(element => {
 element.style.color = 'white'
});

Is there a right way(without using JQuery)?

i want to change color all 'p' elements, i tried do it with this code but it not works.

document.getElementsByTagName('p').forEach(element => {
 element.style.color = 'white'
});

Is there a right way(without using JQuery)?

Share Improve this question asked Jun 23, 2018 at 21:09 H.DenisonH.Denison 531 silver badge5 bronze badges 4
  • document.querySelectorAll('p').style.color = 'white' try that way. – Vladimir Jovanović Commented Jun 23, 2018 at 21:14
  • ^ this returns a NodeList, not an HTMLCollection, which can be iterated over. You can't just set the style.color for the NodeList, though! – chemicalcrux Commented Jun 23, 2018 at 21:16
  • 1 Yeah, you're right @chemicalcrux. My bad. But this code works document.querySelectorAll('p').forEach(e => e.style.color = "white") – Vladimir Jovanović Commented Jun 23, 2018 at 21:17
  • First, that is not jQuery, second, how is that code piece not working? ... Provide a code snippet reproducing the issue described. – Asons Commented Jun 23, 2018 at 21:47
Add a ment  | 

5 Answers 5

Reset to default 3

You can acplish this using the vanilla JavaScript ES2015 (ES6) code.

document.querySelectorAll('p').forEach(e => e.style.color = "white");

UPDATE:

I forgot to mention that this is applying white color of the element in the DOM which is not so efficient, so you can also do this in another way.

CSS:

.white {
  color: #fff;
}

JavaScript:

// If you have just one class for the element
document.querySelectorAll('p').forEach(e => e.className = 'white');

// If you are appending the class of `white`
document.querySelectorAll('p').forEach(e => e.className += ' white'); 

You can do the same like this

var list= document.getElementsByTagName('p');
for (var i = 0; i < list.length; i++) {
    list[i].style.color = "white";
}

As document.getElementsByTagName returns HTMLCollection type object you have to iterate using above method as per this answer

You can't iterate over the HTMLCollection returned by getElementsByTagName. Converting it to an Array will work, though. Use Array.from() to do this:

Array.from(document.getElementsByTagName('p')).forEach(element => { element.style.color = 'white' });

All you have to do here is to make a simple loop.

let el = document.getElementsByTagName('p')
let i

for (i = 0; i < el.length; i++) {
    el[i].style.color = 'white'
}
document.getElementsByTagName('p')

Return HTMLCollection(6) [p, p, p, p, p, p.left] This is a HTML Collection. So if you check

document.getElementsByTagName('p') instanceof Array 

It returns false.

As HTML Collections are not array's so you cannot perform .forEach().

So you must convert it into an array by using Array.from().

The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object.

Array.from(document.getElementsByTagName('p')).forEach(element => {
 element.style.color = 'white'
});

So this is the solution for your problem.

发布评论

评论列表(0)

  1. 暂无评论