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

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

無法清除樣式,請問哪里出錯了

<!DOCTYPE?HTML>
<html>
<head>
<meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8">
<title>className屬性</title>
<style>
????body{?font-size:16px;}
????.one{
????????border:1px?solid?#eee;
????????width:230px;
????????height:50px;
????????background:#ccc;
????????color:red;
????}
????.two{
????????border:1px?solid?#ccc;
????????width:230px;
????????height:50px;
????????background:#9CF;
????????color:blue;
????}
????</style>
</head>
<body>
????<p?id="p1"?>?JavaScript使網頁顯示動態效果并實現與用戶交互功能。</p>
????<input?type="button"?value="添加樣式"?onclick="add()"/>
????<p?id="p2"?class="one">JavaScript使網頁顯示動態效果并實現與用戶交互功能。</p>
????<input?type="button"?value="更改外觀"?onclick="modify()"/>
????<input?type="button"?value="清除樣式"?onclick="clean()"/>
????<script?type="text/javascript">
???????function?add(){
??????????var?p1?=?document.getElementById("p1");
??????????p1.className?=?"one";
???????}
???????function?modify(){
??????????var?p2?=?document.getElementById("p2");
??????????p2.className?=?"two";
???????}
???????function?clean(){
?????????var?deletestyle?=?confirm('是否清除樣式')
?????????if(deletestyle?==?true){
?????????????document.getElementById("p2").removeAttribute("style");
?????????}
????????else{
????????????alert("啥都不做");
????????}
???????}
????</script>
</body>
</html>


正在回答

5 回答

removeAttribute()方法用來刪除指定的屬性,是.style的,在<style></style>標簽里面改的樣式好像重置不了

不知道是不是這樣

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

烜烜 提問者

http://www.w3school.com.cn/tiy/t.asp?f=jsref_element_removeattribute 我看了一下W3C,是這樣說的: removeAttribute() 方法刪除指定的屬性。 此方法與 removeAttributeNode() 方法的差異是:removeAttributeNode() 方法刪除指定的 Attr 對象,而此方法刪除具有指定名稱的屬性。結果是相同的。同時此方法不返回值,而 removeAttributeNode() 方法返回被刪除的屬性,以 Attr 對象的形式。
2016-01-23 回復 有任何疑惑可以回復我~
#2

烜烜 提問者

非常感謝!
2016-01-26 回復 有任何疑惑可以回復我~

樓主我知道了 ,在<style></style>標簽里面改的樣式是清除不了的,可是你知道怎么改嗎

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

請問樓主這個問題如何解決啊,我也是有這個困擾

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

也許是因為。。你removeAttribute("style"),style的值沒有寫在P1和P2標簽里面。是寫在head上面的。所以。我覺得可以這樣寫

function clean(){

? ? ? ? ?var deletestyle = confirm('是否清除樣式')

? ? ? ? ?if(deletestyle == true)

? ? ? ? ?{

? ? ? ? ? ? ?p1.className = "";

? ? ? ? ? ? ?p2.className = "one";

? ? ? ? ?}

? ? ? ? else{

? ? ? ? ? ? alert("啥都不做");

? ? ? ? }


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

烜烜 提問者

也就是說removeAttribute("style"),能清除行內樣式,不能清除內聯樣式吧:)
2016-01-23 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

無法清除樣式,請問哪里出錯了

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

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

幫助反饋 APP下載

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

公眾號

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