在文本字段中部分选择文本 [英] Partially select text inside text field
本文介绍了在文本字段中部分选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的脚本只选择文本字段中的部分文本(< input type =text
)。我该怎么做?
I want my script to select just a part of the text inside a text field (<input type="text"
). How can I do this?
推荐答案
您将需要使用Range,并且跨浏览器兼容性可能会成为问题。
You will need to use Range, and cross-browser compatibility will probably be an issue.
Quirksmode:创建一个Range对象来自Selection对象
Quirksmode: Creating a Range object from a Selection object
如果jQuery是一个选项,这里有一个你可以使用的函数(参考)...
If jQuery is an option, here is a function you can use (reference)...
$.fn.selectRange = function(start, end) {
return this.each(function() {
if(this.setSelectionRange) {
this.focus();
this.setSelectionRange(start, end);
} else if(this.createTextRange) {
var range = this.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
});
};
这篇关于在文本字段中部分选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文