admin管理员组文章数量:1415684
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";
本文标签: How to change HTML style by taking the class name using javascript (no jQuery allowed)Stack Overflow
版权声明:本文标题:How to change HTML style by taking the class name using javascript (no jQuery allowed)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196959a2647188.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论