為什么我用removeAttribute("style")來恢復(fù)樣式不行呢,
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>讓我們互動下</title>
<style type="text/css">
P{
border:1px solid red;
background:gray;
width:100px;
height:30px;
text-align:center;
line-height:30px;
}
.one{
? border:2px solid blue;
? background:yellow;
? width:200px;
? height:50px;
? line-height:50px;
}
.two{
border:3px dashed yellow;
background:purple;
width:300px;
height:300px;
line-height:300px;
}
</style>
<script type="text/javascript">
function rec(){
var message=confirm("關(guān)注JS進(jìn)階篇");
if (message==true)?
{
document.write("希望你學(xué)有所成");
}?
else?
{
document.write("希望你能關(guān)注一下");
}
}
</script>
</head>
<body>
<p id="con">JS進(jìn)階篇</p>
<form>
<input type="button" name="button" onclick="rec()" value="點(diǎn)擊我試試" />
<input type="button" onclick="add()" value="增加樣式" />
<input type="button" onclick="change()" value="改變樣式" />
<input type="button" name="button" onclick="resetcss()" value="重置樣式" />
</form>
<script type="text/javascript">
var mychar=document.getElementById("con");
function add(){
mychar.className="one";
}
function change(){
mychar.className="two";
}
function resetcss(){
var message1=confirm("確定要恢復(fù)原始設(shè)置?");
if (message1==true)?
{
mychar.removeAttribute("style");
}?
}
</script>
</body>
</html>
2016-07-31
mychar.removeAttribute("style");//這里移除的style是節(jié)點(diǎn)里面聲明的style,而不能移除className所帶來的css樣式,假如<p id="con" style=“color:red;”>JS進(jìn)階篇</p> 那么按了重置,red就會沒有。
修改:
mychar.removeAttribute("class");
2016-09-15
? ? //mychar.removeAttribute("class");
? ? mychar.classList.remove("one"&&"two");
這兩句都可以的
2016-07-31
style用單引號試試
2016-07-31
你把 if else 語句補(bǔ)完整試試?
2016-07-31
你吧style煥成單引號試試 ?''