再談javascript常見錯(cuò)誤及解決方法
初學(xué)Javascript,每天總是被很小的問題折磨半天,今晚就有好幾個(gè)小問題。
第一:全部使用雙引號造成匹配錯(cuò)誤
<input type="checkbox" onmouseover="document.getElementById("test").style.display="none":"/>
改行一直報(bào)錯(cuò)誤:unexpected toke “}” 檢查半天也沒有發(fā)現(xiàn)錯(cuò)誤,對照發(fā)現(xiàn)視頻上是使用單引號
<input type="checkbox" onmouseover="document.getElementById('test').style.display="none":"/>
改成單引號后錯(cuò)誤總算消除,困擾我一夜晚。。附上鏈接http://www.cnblogs.com/chinabc/archive/2010/11/19/1881947.html
第二:錯(cuò)誤添加分號
<div id="test" class="test1" onmouseover="toYellow()" ;onmouseout="toRed()";>change</div>
多寫了一個(gè)分號,導(dǎo)致分號后的代碼不執(zhí)行
第三:函數(shù)名后多寫括號
<script> function toYellow(){ document.getElementById("test").className="test2"; } function toRed(){ document.getElementById("test").className="test1"; } document.getElementById("test").onmouseover=toYellow(); document.getElementById("test").onmouseout=toRed(); </script>
卻掉 toYellow()和 toRed()后的括號后正常執(zhí)行
第四:checkbox的checked屬性修改
用三個(gè)按鈕實(shí)現(xiàn)checkbox的全選、全部選、反選。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <button id="btn">全選</button> <button id="nobtn">全不選</button> <button id="inverse">反選</button><br /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <input type="checkbox" /> <script> var btn=document.getElementById("btn"); var input=document.getElementsByTagName("input"); btn.onclick=function(){ for(var i=0;i<input.length;i++){ input[i].checked="checked"; } } var nobtn=document.getElementById("nobtn"); nobtn.onclick=function(){ for(var i=0;i<input.length;i++){ input[i].checked=false; } } var inverse=document.getElementById("inverse"); inverse.onclick=function(){ for(var i=0;i<input.length;i++){ if(input[i].checked==false){ input[i].checked=true; }else{ input[i].checked=false; } } } </script> </body> </html>
以上這篇再談javascript常見錯(cuò)誤及解決方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
uniapp自定義驗(yàn)證碼輸入框并隱藏光標(biāo)
這篇文章主要介紹了uniapp自定義驗(yàn)證碼輸入框隱藏光標(biāo),效果是點(diǎn)擊輸入框喚起鍵盤,藍(lán)框就相當(dāng)于input的光標(biāo),驗(yàn)證碼輸入錯(cuò)誤或者不符合格式要求會(huì)將字體以及邊框改成紅色提示持續(xù)1s然后清空數(shù)據(jù),恢復(fù)原邊框樣式,需要的朋友可以參考下2023-02-02js 兩數(shù)組去除重復(fù)數(shù)值的實(shí)例
下面小編就為大家分享一篇js 兩數(shù)組去除重復(fù)數(shù)值的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12使用有限狀態(tài)機(jī)實(shí)現(xiàn)簡版的html解析器
FSM(Finite State Machines) 有限狀態(tài)機(jī),也叫有限狀態(tài)自動(dòng)機(jī),是為研究有限內(nèi)存的計(jì)算過程和某些語言類而抽象出的一種計(jì)算模型,本文將使用有限狀態(tài)機(jī)實(shí)現(xiàn)一個(gè)簡版的html解析器,有需要的小伙伴可以參考下2023-11-11JavaScript實(shí)例--創(chuàng)建一個(gè)歡迎cookie
這篇文章主要介紹了JavaScript實(shí)例--創(chuàng)建一個(gè)歡迎cookie,2022-01-01