為什么我這里設(shè)置的取消按鈕是無效的呢?
<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使網(wǎng)頁顯示動態(tài)效果并實現(xiàn)與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
? ??
? ? <input type="button" value="取消設(shè)置" onclick="quxiao()"/>
<script type="text/javascript">
? function add(){
? ? ?var p1 = document.getElementById("p1");
? ? ?p1.className="one";
? }
//定義取消設(shè)置?
? ? ? function quxiao(){
? ? ? ? var a = document.getElementById("p1");
? ? ? ? var qx=confirm("是否取消");
? ? ? ? if(qx==true){
? ? ? ? a.removeAttribute('style');?
? ? }
}
</script>
2016-06-25
a.removeAttribute('class');因為它沒有style,只有class,你的style是通過class添加的
2016-06-22
?a.removeAttribute('style');?這句是無效的,改為?a.className="";。
因為?var a = document.getElementById("p1");而<p id="p1">中并無style屬性。
樣式的添加是通過給對象的className屬性賦值實現(xiàn)的,要取消樣式就要通過給className賦值為空。
如果樣式添加是通過setAttribute("style","color:red")方法實現(xiàn)的,removeAttribute('style')就起作用了。當(dāng)然由于setAttribute()方法存在兼容性問題,很少用這種方法實現(xiàn)樣式添加。
2016-06-21
? a.removeAttribute('style');?這句有問題吧,換成a.className="";?? 就可以了,我也不知道為什么
2016-06-21
我也不懂。。。。