為什么不變色- - ?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>我們互動下</title>
?<style type="text/css">
? ? p{
? ? ? ? width:150px;
? ? ? ? height:50px;
? ? ? ? border:1px solid #333;
? ? ? ? padding:5px;
? ? }
?</style>
? <script type="text/javascript">
? ? var myvalue = document.getElementById("con");
? ? function levelup(){
? ? ? ? var mine = confirm("實驗階段");
? ? ? ? if (mine=true){
? ? ? ? ? ? myvalue.style.color="red";
? ? ? ? ? ? myvalue.style.backgroundColor="pink";
? ? ? ? }
? ? ? ? else{
? ? ? ? ? ? document.write("Suprise!");
? ? ? ? }
? ? }
? </script>
</head>
<body>
?<p id="con">js升級篇</p>
?<form>?
? ? <input type="button" value="點開有驚喜" onclick="levelup()">
?</form>
</body>
</html>
2015-11-12
js是從上到下 從左至右開始開始渲染的,也就是說瀏覽器是開始解析你的js再開始構建html的。
另外 還有個問題??if (mine=true){ ?這里應該是if (mine==true){ ?
改進方法 1: 將你的script放在body后面
方法2 :使用DOMReady
方法3:window.onload(不推薦,僅測試)
2015-11-12
樓上說得對
2015-11-12
因為你的myvalue.style.color='red';是捕獲不到的