欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jquery聚焦文本框與擴(kuò)展文本框聚焦方法

 更新時(shí)間:2012年10月12日 11:05:33   作者:  
在不同的瀏覽器中,一個(gè)文本框,如果只是直接給文本框設(shè)置focus(),那么光標(biāo)聚焦的位置可能是在最前面。下面的代碼則是給jquery擴(kuò)展一個(gè)textFocus方法
光標(biāo)聚焦的位置在最前面
復(fù)制代碼 代碼如下:

<!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>
<title>jquery聚焦文本框 -腳本之家</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
</head>
<body>
<form action="http://www.baidu.com" id="cse-search-box">
<div>
<input type="hidden" name="cx" value="partner-pub-7740261255677392:7064996710" />
<input type="hidden" name="ie" value="UTF-8" />
<!--文本框--><input type="text" name="q" size="25" />
<input type="submit" name="sa" value="&#x641c;&#x7d22;" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$("input[name='q']").focus();
})</script>
</body>
</html>


jquery擴(kuò)展文本框聚焦方法

在不同的瀏覽器中,一個(gè)文本框,如果只是直接給文本框設(shè)置focus(),那么光標(biāo)聚焦的位置可能是在最前面。下面的代碼則是給jquery擴(kuò)展一個(gè)textFocus方法,用于聚焦文本框,并使光標(biāo)在最后,使用$("input").textFocus()。也可以傳入一個(gè)數(shù)字參數(shù),設(shè)置光標(biāo)聚焦的位置。如$("input").textFocus(2),則光標(biāo)在在第二個(gè)字符后面。
復(fù)制代碼 代碼如下:

(function($){
$.fn.textFocus=function(v){
var range,len,v=v===undefined?0:parseInt(v);
this.each(function(){
if($.browser.msie){
range=this.createTextRange(); //文本框創(chuàng)建范圍
v===0?range.collapse(false):range.move("character",v); //范圍折疊
range.select(); //選中
}else{
len=this.value.length;
v===0?this.setSelectionRange(len,len):this.setSelectionRange(v,v); //dom直接設(shè)置選區(qū),然后focus
}
this.focus();
});
return this;
}
})(jQuery)

相關(guān)文章

最新評(píng)論