I tried this so far and if I take the element id is working but if I take the class name is not working...
<!DOCTYPE html>
<html>
<body>
<p id="p1" class="theClass">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
document.getElementsByClassName("theClass").style.color = "blue";
document.getElementById("p2").style.fontSize = "larger";
</script>
<p>The paragraph above was changed by a script.</p>
</body>
</html>
This is the output:
I tried this so far and if I take the element id is working but if I take the class name is not working...
<!DOCTYPE html>
<html>
<body>
<p id="p1" class="theClass">Hello World!</p>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
document.getElementsByClassName("theClass").style.color = "blue";
document.getElementById("p2").style.fontSize = "larger";
</script>
<p>The paragraph above was changed by a script.</p>
</body>
</html>
This is the output:
Share Improve this question asked May 6, 2018 at 1:25 user6363467user6363467 1241 silver badge10 bronze badges3 Answers
Reset to default 3getElementsByClassName
returns an HTMLCollection, not an element. If you want to apply a style to an element in the collection, select that element first:
document.getElementsByClassName("theClass")[0].style.color = "blue";
<p id="p1" class="theClass">Hello World!</p>
But if you're going to select a single element, it would be much better to use querySelector
:
document.querySelector('.theClass').style.color = "blue";
<p id="p1" class="theClass">Hello World!</p>
Even if you did need to apply a style (or do something) to multiple elements with a class name in mon, the getElementsBy* methods return HTMLCollections, which can be difficult to work with. Consider using querySelectorAll instead, which returns a static NodeList - unlike an HTMLCollection, it can be iterated over directly, it won't change while it's being iterated over, and it's much more flexible.
document.querySelectorAll('.theClass')
.forEach(p => p.style.color = "blue");
<p class="theClass">Hello World!</p>
<p class="theClass">Hello World!</p>
<p class="theClass">Hello World!</p>
document.getElementsByClassName
give you the list of the nodes. And there is no style
property on the list. You'll have to iterate through the list to apply styles.
If there is just on div there you can use
document.getElementsByClassName("theClass")[0].style.color = "red"
PS. You should always check your browser's console when something is not working. You'll be seeing an error like can not ... .style of ...
document.getElementById("p2").style.color = "blue";
document.getElementsByClassName("theClass")[0].style.color = "blue";
document.getElementById("p2").style.fontSize = "larger";
var els = document.getElementsByClassName('theClass1');
for(var i=0; i< els.length; i++){
els[i].style.color = "green"
}
<!DOCTYPE html>
<html>
<body>
<p id="p1" class="theClass">Hello World!</p>
<p id="p2">Hello World!</p>
<p id="p12" class="theClass1">Hello World 1!</p>
<p id="p14" class="theClass1">Hello World 2!</p>
<p id="p13" class="theClass1">Hello World 3!</p>
<p>The paragraph above was changed by a script.</p>
</body>
</html>
Try this:
document.getElementsByClassName("theClass")[0].style.color = "blue";
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745196959a4616141.html
评论列表(0条)