jquery聚焦文本框與擴展文本框聚焦方法
更新時間:2012年10月12日 11:05:33 作者:
在不同的瀏覽器中,一個文本框,如果只是直接給文本框設(shè)置focus(),那么光標(biāo)聚焦的位置可能是在最前面。下面的代碼則是給jquery擴展一個textFocus方法
光標(biāo)聚焦的位置在最前面
<!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="搜索" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$("input[name='q']").focus();
})</script>
</body>
</html>
jquery擴展文本框聚焦方法
在不同的瀏覽器中,一個文本框,如果只是直接給文本框設(shè)置focus(),那么光標(biāo)聚焦的位置可能是在最前面。下面的代碼則是給jquery擴展一個textFocus方法,用于聚焦文本框,并使光標(biāo)在最后,使用$("input").textFocus()。也可以傳入一個數(shù)字參數(shù),設(shè)置光標(biāo)聚焦的位置。如$("input").textFocus(2),則光標(biāo)在在第二個字符后面。
(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)
復(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="搜索" />
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$("input[name='q']").focus();
})</script>
</body>
</html>
jquery擴展文本框聚焦方法
在不同的瀏覽器中,一個文本框,如果只是直接給文本框設(shè)置focus(),那么光標(biāo)聚焦的位置可能是在最前面。下面的代碼則是給jquery擴展一個textFocus方法,用于聚焦文本框,并使光標(biāo)在最后,使用$("input").textFocus()。也可以傳入一個數(shù)字參數(shù),設(shè)置光標(biāo)聚焦的位置。如$("input").textFocus(2),則光標(biāo)在在第二個字符后面。
復(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)文章
jqPaginator結(jié)合express實現(xiàn)分頁展示內(nèi)容效果
這篇文章主要為大家詳細(xì)介紹了jqPaginator結(jié)合express實現(xiàn)分頁展示內(nèi)容效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04jQuery+datatables插件實現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能示例
這篇文章主要介紹了jQuery+datatables插件實現(xiàn)ajax加載數(shù)據(jù)與增刪改查功能,涉及jQuery結(jié)合datatables插件針對頁面表格實現(xiàn)數(shù)據(jù)加載及增刪改查等相關(guān)操作技巧,需要的朋友可以參考下2018-04-04jQuery實現(xiàn)可拖拽的許愿墻效果【附demo源碼下載】
這篇文章主要介紹了jQuery實現(xiàn)可拖拽的許愿墻效果,可實現(xiàn)拖拽圖片與層疊顯示功能,涉及jQuery插件的簡單使用,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-09-09jquery DataTable實現(xiàn)前后臺動態(tài)分頁
本篇文章主要介紹了jquery DataTable實現(xiàn)前后臺動態(tài)分頁。小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06jQuery Validate表單驗證插件 添加class屬性形式的校驗
這篇文章主要介紹了jQuery Validate表單驗證插件,在class屬性中添加校驗規(guī)則進行簡單的校驗,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01Jquery 數(shù)據(jù)選擇插件Pickerbox使用介紹
目前市面上很少見或幾乎沒有這數(shù)據(jù)(對象)選擇插件.比如,點擊input , select 元素時彈出div(窗口),載入數(shù)據(jù)讓用戶選擇數(shù)據(jù),選擇后在填充回對應(yīng)的元素.2012-08-08