jQuery中有一個(gè)focus()方法能設(shè)置對象的焦點(diǎn),在1.7以下的版本中,不管對象是不是disabed狀態(tài),這個(gè)方法都不會(huì)報(bào)錯(cuò)(只是當(dāng)disabled時(shí),設(shè)置焦點(diǎn)的代碼無效),但在1.7版本中,如果對象是disabled狀態(tài),這時(shí)調(diào)用focus()方法時(shí),會(huì)直接報(bào)異常:
Error: Can't move focus to the control because it is invisible, not enabled, or of a type that does not accept the focus.
意思是:不可見或不可用的元素?zé)o法獲取焦點(diǎn)。(特別提一下:IE9很NB,能自動(dòng)識(shí)別這種情況,在IE9下不會(huì)報(bào)錯(cuò),但是IE9以下的版本全掛。)
復(fù)制代碼 代碼如下:
<!doctype html>
<html>
<head>
<title>測試</title>
<script src="jquery-1.7.min.js" type="text/javascript"></script>
<!--<script src="jquery-1.4.4.min.js" type="text/javascript"></script>-->
<script type="text/javascript">
function fnTest(){
//try{
$("#txt").focus();
//}catch(e){}
}
</script>
</head>
<body>
<div>
<input type="text" disabled="disabled"/>
<input type="text"/>
<input type="button" value="Test"/>
</div>
</body>
</html>
建議:
如果一定要用最高版本的jQuery,最省事的辦法莫過于在寫xxx.focus()時(shí),加一個(gè)try/catch,變成try{xxx.focus();}catch(e){}
新聞熱點(diǎn)
疑難解答
圖片精選