2 回答

TA貢獻1840條經驗 獲得超5個贊
首先:我們要糾正一個錯誤,相同id屬性,一個頁面最好只有一個。在你提供的代碼中,出現了3個flashit是不正確的。相同的class屬性是可以出現多個的。
其次:在for循環中,使用setInterval函數時,里面的變量 i 不能放在雙引號中,那樣它會被識別為字符串的,需要用“+”連接符對變量進行連接。請參考下面的示例。
根據你的代碼,我做了適當修改。
示例效果為,紅藍顏色替換,你可以根據實際需求做一些適當變更。代碼如下:
HTML代碼:
12345678 | < form id = "form1" > < textarea cols = "20" rows = "8" name = "flashit1" class = "flashit" class = "flashit" style = "color:blue" >閃爍的文字用來強調一些重要的文字 </ textarea > < input type = "text" value = "文本框也可以" name = "flashit" class = "flashit" style = "color:blue" > < input type = "submit" name = "flashit1" class = "flashit" style = "color:blue" > </ form > |
JavaScript代碼:
123456789101112 | var flashelement=document.querySelectorAll( "input, textarea" ); for (i = 0; i < flashelement.length; i++){ setInterval( "changecolor(" + i + ")" ,1000); } function changecolor(which){ if (flashelement[which].style.color== "blue" ){ flashelement[which].style.color= "red" ; } else { flashelement[which].style.color= "blue" ; } } |
運行結果:

TA貢獻1851條經驗 獲得超5個贊
代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Test</title>
<script src="" type="text/javascript" charset="utf-8"></script>
<style>
*{
/*margin: 0px;
padding: 0px;*/
}
.test1{
width: 100%;
height: 22px;
line-height: 22px;
border: 2px solid #ccc;
}
</style>
</head>
<body>
<div class="test1">公告內容</div>
<br />
<button class="changestyle">更改樣式</button>
<script>
$(function(){
$(".changestyle").off("click").on("click",function(){
$(".test1").css({
"font-size":"16px",
"font-weight":"bold",
"border":"2px solid blue",
"width":"200px",
"height":"100px",
"text-align":"center",
"line-height":"100px",
"color":"red"
});
});
});
</script>
</body>
</html>
望~~!
- 2 回答
- 0 關注
- 438 瀏覽
添加回答
舉報