我編寫了以下JS爲什麼JS錯誤在IE6(未變種變量)
<html>
<body>
<img id="img" src="http://example.com/img.jpg" />
<script type="text/javascript">
//<![CDATA[
(function(){
img = document.getElementById("img");
img.src = "http://example.com/img.png";
})();
//]]>
</script>
</body>
但IE6 一些JS錯誤發生。
因爲我應該使用var img嗎? 順帶
<body>
<img id="img" src="http://example.com/img.jpg" />
<script type="text/javascript">
//<![CDATA[
(function(){
var img = document.getElementById("img");
img.src = "http://example.com/img.png";
})();
//]]>
</script>
</body>
</html>
是我不能得到之所以 你能解釋我沒有問題 ?
+1:用於解釋全局變量 – 2010-12-10 09:11:57