![](/img/trans.png)
[英]Changing text colour of Html (H2) tag while mouseover in any picture?
[英]Changing the colour of a <b> tag
我正在嘗試使用JavaScript將文本的顏色更改為綠色,但收到警告,提示它是錯誤的對象,腳本崩潰。
<html>
<head>
<style>
b {
color: #0000FF;
}
</style>
<script>
function resizea() {
var a = document.getElementsByTagName("b");
a.style.color = "#00FF00";
}
</script>
</head>
<body onload="resizea()">
<b>I am blue</b>
</body>
</html>
由於getElementsByTagName("b")
返回元素的集合,因此您需要獲取第一個元素:
var a = document.getElementsByTagName("b")[0];
注意括號之間的索引[0]
更詳細的方法,但希望更清晰。 遍歷所有b
元素並更改其顏色:
var boldTags = document.getElementsByTagName ("b");
console.log("There are " + boldTags.length + " bold elements");
for (var i = 0; i < boldTags.length; i++) {
var boldTag = boldTags[i];
boldTag.style.color = "#00FF00";
console.log("Element indexed " + (i + 1) + " has color of \n" + boldTag.style.color);
}
如果頁面中有多個<b>
標記,則可以使用以下代碼更改所有粗體文本的顏色。
$("b").attr("style","color:#00FF00;")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.