為什么加的重置按鈕無法重置,求大神看看原因
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<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>
? ? <div id=txt>
? ? <p id="p1" > JavaScript使網頁顯示動態(tài)效果并實現與用戶交互功能。</p>
? ? <input type="button" value="添加樣式" onclick="add()"/>
<p id="p2" class="one">JavaScript使網頁顯示動態(tài)效果并實現與用戶交互功能。</p>
? ? <input type="button" value="更改外觀" onclick="modify()"/>
? ? <input type="button" value="重置" onclick="rest()"/>
? ? </div>
? ??
<script type="text/javascript">
var mydiv=document.getElementById("txt");
? ?function add(){
? ? ? var p1 = document.getElementById("p1");
? ? ? p1.className="one";
? ?}
? ?function modify(){
? ? ? var p2 = document.getElementById("p2");
? ? ? p2.className="two";
? ?}
? ?function rest(){
? ? ? ?var myrest=confirm("是否重置");
? ? ? ?if(myrest==true){
? ? ? ? ? ?mydiv.removeAttribute("style");
? ? ? ?}
? ?}
</script>
</body>
</html>
2020-03-03
因為Css會一直影響整個DOM,除非你用行內樣式覆蓋掉原來的css樣式;你remove掉style是沒用的,因為那個元素沒有style
2020-02-28
mydiv.removeAttribute("style");
"style"改成'style'
屬性值用單引號
2020-02-28
重置是type=reset吧