請教前輩個小問題!
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>location</title> </head> ?<script?type="text/javascript"> ?????document.write(window.location.href);??? ????? ?</script> </head> <body> </body> </html>
上面這段代碼是正確的可以運行的
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>location</title> </head> ?<script?type="text/javascript"> ?????document.write(var?w=window.location.href);??? ????? ?</script> </head> <body> </body> </html>
這第二段代碼就不能正確運行了,僅僅是相比于第一段代碼修改了第8行而已。
請問這其中又什么規矩嗎?何時能這樣用(類似于第二段代碼)何時不能?
類似的還有一段代碼都是可以按照上面的第二段代碼的書寫方式寫的而且可以成功運行:
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>計時器</title> <script?type="text/javascript"> ???function?clock(){ ??????var?time=new?Date();????????????????????? ??????document.getElementById("clock").value?=?time; ???} //?每隔100毫秒調用clock函數,并將返回值賦值給i ?????var?i=window.setInterval("clock()",100); ?????? </script> </head> <body> ??<form> ????<input?type="text"?id="clock"?size="50"??/> ????<input?type="button"?value="Stop"?onclick="var?inn=window.clearInterval(i)"??/> ??</form> </body> </html>
這上面的這段代碼的第19行是可以這么寫的,這到底什么時候能直接按照下面的定義變量的方法?
<input type="button" value="Stop" onclick="var inn=window.clearInterval(i)" ?/>
這另有一段基于第三段代碼的改寫,僅僅修改了第19行,格式同第一段代碼,竟然也可以運行,
<!DOCTYPE?HTML> <html> <head> <meta?http-equiv="Content-Type"?content="text/html;?charset=utf-8"> <title>計時器</title> <script?type="text/javascript"> ???function?clock(){ ??????var?time=new?Date();????????????????????? ??????document.getElementById("clock").value?=?time; ???} //?每隔100毫秒調用clock函數,并將返回值賦值給i ?????var?i=window.setInterval("clock()",100); ?????? </script> </head> <body> ??<form> ????<input?type="text"?id="clock"?size="50"??/> ????<input?type="button"?value="Stop"?onclick="window.clearInterval(i)"??/> ??</form> </body> </html>
我實在總結不出規律來了,找了一些資料也沒弄明白,,希望前輩給我解惑答疑!
2015-04-27
第一個問題,document.write(var?w=window.location.href); ? 你這么寫是想表達個啥意思??是定義變量呢?還是打印定義的變量?這樣寫,計算機看不懂,要不你就寫document.write(window.location.href); ,要不你就這樣var?w=window.location.href;document.write(w);
第二個問題這兩個是不一樣的,因為你下面這個是調用的事件~~什么是事件,事件就是一個函數或者多個函數。所以這么些也可以的。就規范來說,最好不要這樣寫,可能存在兼容性問題。