各位大神,這個有什么錯誤的地方呢
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>重置</title>
<!--引入外部文件的方式-->
<script type="text/javascript">
?? ?
</script>
</head>
<body>
<p id="con">這是一首簡單的小情歌</p>
<input type="button" value="重置" onclick="add2()">
<script type="text/javascript">
??? function add()
??? {var char=document.getElementById("con");
?? ?char.style.color="red";
?? ?char.style.background="yellow";
?? ?char.style.font-size="30";
?? ?char.style.width="600";}
?? ?function add2(){
?? ?var gh=confirm("確定重置嗎");
?? ?if(gh==true)
?? ?{
?? ??? ?char.removeAttribute("style")}}
</script>
</body>
</html>
2016-11-22
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>重置</title>
</head>
<body>
<p id="con">這是一首簡單的小情歌</p>
<input type="button" value="重置" onclick="add2()">
<script type="text/javascript">
var char=document.getElementById("con");
? ? char.style.color="red";
char.style.color="red";
? ? char.style.background="yellow";
? ? char.style.fontSize="30";
? ? char.style.width="600";
function add2(){
var gh=confirm("確定重置嗎");
? ? if(gh==true){
char.removeAttribute("style");
}
}
</script>
</body>
</html>
你之前寫的代碼定義了兩個函數add()和add2(),第一個函數add()用來加載樣式,可是這個函數你定義了并未調用,而且并未繼承給add2(),所以add()這個函數里的變量及內容是不生效的,導致add2()里面的char變量根本存在。還有一個小小的細節問題:char.style.font-size="30"; ?這行代碼寫錯了,應該是這樣:char.style.fontSize="30"; ?
2016-11-22
設置元素字體大小的屬性是fontSize,另外你的add2()并沒有被調用過
2016-11-21
不能定義char吧