亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

想了解一下,改變dom的style?

想了解一下,改變dom的style?

精慕HU 2021-02-17 12:11:48
<html><head><title>文字閃爍</title></head><body><form id="form1"><div id="a">文本框也可以</div><textarea cols="20" rows="8" name="flashit1" id="flashit" style="color:blue">閃爍的文字用來強調一些重要的文字</textarea><br><input type="text" value="文本框也可以" name="flashit" id="flashit" style="color:blue"><br><input type="submit" name="flashit1" id="flashit" style="color:blue"></form><script type="text/javascript">var flashelement=document.getElementById("form1");for(i=0;i<flashelement.flashit.length;i++){var tempvariable=setInterval("changecolor(i)",1000);}function changecolor(which){if(flashelement.flashit[which].style.color==""){alert("asd");flashelement.flashit[which].style.color="red";}else{flashelement.flashit[which].style.color="";}}</script></body></html>這個if判斷里為什么上style 會報錯?
查看完整描述

2 回答

?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

首先:我們要糾正一個錯誤,相同id屬性,一個頁面最好只有一個。在你提供的代碼中,出現了3個flashit是不正確的。相同的class屬性是可以出現多個的。

其次:在for循環中,使用setInterval函數時,里面的變量 不能放在雙引號中,那樣它會被識別為字符串的,需要用“+”連接符對變量進行連接。請參考下面的示例。

根據你的代碼,我做了適當修改。

示例效果為,紅藍顏色替換,你可以根據實際需求做一些適當變更。代碼如下:

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代碼:

123456789101112var 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";    }}

運行結果:



查看完整回答
反對 回復 2021-03-17
?
江戶川亂折騰

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>
望~~!



查看完整回答
反對 回復 2021-03-17
  • 2 回答
  • 0 關注
  • 438 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號