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

為了賬號安全,請及時綁定郵箱和手機立即綁定

為什么我這里設置的取消按鈕是無效的呢?

<style>

? ? body{ font-size:16px;}

? ? .one{

border:1px solid #eee;

width:230px;

height:50px;

background:#ccc;

color:red;

? ? }


</style>

</head>

<body>

? ? <p id="p1"> JavaScript使網頁顯示動態效果并實現與用戶交互功能。</p>

? ? <input type="button" value="添加樣式" onclick="add()"/>

? ??

? ? <input type="button" value="取消設置" onclick="quxiao()"/>


<script type="text/javascript">

? function add(){

? ? ?var p1 = document.getElementById("p1");

? ? ?p1.className="one";

? }

//定義取消設置?

? ? ? function quxiao(){

? ? ? ? var a = document.getElementById("p1");

? ? ? ? var qx=confirm("是否取消");

? ? ? ? if(qx==true){

? ? ? ? a.removeAttribute('style');?

? ? }

}

</script>


正在回答

4 回答

a.removeAttribute('class');因為它沒有style,只有class,你的style是通過class添加的

1 回復 有任何疑惑可以回復我~

?a.removeAttribute('style');?這句是無效的,改為?a.className="";。

因為?var a = document.getElementById("p1");而<p id="p1">中并無style屬性。

樣式的添加是通過給對象的className屬性賦值實現的,要取消樣式就要通過給className賦值為空。

如果樣式添加是通過setAttribute("style","color:red")方法實現的,removeAttribute('style')就起作用了。當然由于setAttribute()方法存在兼容性問題,很少用這種方法實現樣式添加。

1 回復 有任何疑惑可以回復我~

? a.removeAttribute('style');?這句有問題吧,換成a.className="";?? 就可以了,我也不知道為什么

0 回復 有任何疑惑可以回復我~

我也不懂。。。。

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

為什么我這里設置的取消按鈕是無效的呢?

我要回答 關注問題
微信客服

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

幫助反饋 APP下載

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

公眾號

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