手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >jquery聚焦文本框与扩展文本框聚焦方法
jquery聚焦文本框与扩展文本框聚焦方法
摘要:光标聚焦的位置在最前面复制代码代码如下:jquery聚焦文本框-查字典教程网$(document).ready(function(){$("...

光标聚焦的位置在最前面

复制代码 代码如下:

<!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扩展文本框聚焦方法

在不同的浏览器中,一个文本框,如果只是直接给文本框设置focus(),那么光标聚焦的位置可能是在最前面。下面的代码则是给jquery扩展一个textFocus方法,用于聚焦文本框,并使光标在最后,使用$("input").textFocus()。也可以传入一个数字参数,设置光标聚焦的位置。如$("input").textFocus(2),则光标在在第二个字符后面。

复制代码 代码如下:

(function($){

$.fn.textFocus=function(v){

var range,len,v=v===undefined?0:parseInt(v);

this.each(function(){

if($.browser.msie){

range=this.createTextRange(); //文本框创建范围

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直接设置选区,然后focus

}

this.focus();

});

return this;

}

})(jQuery)

【jquery聚焦文本框与扩展文本框聚焦方法】相关文章:

jQuery插件制作之全局函数用法实例

JQuery+CSS实现图片上放置按钮的方法

jQuery实现不断闪烁文字的方法

js验证上传图片的方法

js实现文本框选中的方法

jQuery仿gmail实现fixed布局的方法

jQuery判断一个元素是否可见的方法

jQuery替换textarea中换行的方法

Jquery解析json字符串及json数组的方法

js实现鼠标移到链接文字弹出一个提示层的方法

精品推荐
分类导航