[英]Selecting a specific subsection of a text input box in Javascript/YUI
I'm trying to programmatically select a substring of text inside of an input text box. 我正在尝试以编程方式在输入文本框中选择文本的子字符串。 I found an example here but there's only one problem. 我在这里找到了一个例子,但是只有一个问题。 It doesn't work! 没用! I tried it on FF4, the latest build of Chrome and IE. 我在FF4(Chrome和IE的最新版本)上进行了尝试。 I haven't tried Opera but if it's not working on those 3 it's not a good enough solution. 我还没有尝试过Opera,但是如果不能在Opera 3上运行,那还不够好。
I also searched on setSelectionRange to see if it's deprecated but I couldn't find anything to lead me to believe that it was. 我还在setSelectionRange上进行了搜索,以查看它是否已被弃用,但是我找不到任何让我相信它的东西。
Here's my code that I'm trying to implement this in: 这是我尝试在其中实现的代码:
_defShowTimeUnitFn: function(e) {
if(!this.isValidInput()) {
var inputNode = this._inputNode,
input = inputNode.get(VALUE),
newText;
newText = Y.Lang.trim(input) + ' ' + SHOW_TIMEUNIT_DEFAULT;
inputNode.set(VALUE, newText);
if (inputNode.setSelectionRange) {
// Mozilla
inputNode.setSelectionRange(0, 1);
} else if (inputNode.createTextRange) {
// IE
var range = inputNode.createTextRange();
range.collapse(true);
range.moveEnd('character', 1);
range.moveStart('character', 1);
range.select();
} else if (inputNode.selectionStart) {
inputNode.selectionStart = 0;
inputNode.selectionEnd = 1;
}
inputNode.focus();
}
},
Does anybody know the commands to accomplish 有人知道要完成的命令吗
textbox.setSelectionRange(start,end);
in a modern browser? 在现代浏览器中?
setSelectionRange()
works on all major browsers except IE <= 8. Since get()
and set()
are not standard methods of DOM elements, I'm pretty sure inputNode
is not a reference to an actual <input>
element. setSelectionRange()
在IE <= 8之外的所有主流浏览器上均可使用。由于get()
和set()
不是DOM元素的标准方法,因此我很确定inputNode
不是对实际<input>
元素的引用。 I imagine it's a YUI wrapper object around an <input>
element. 我想象这是一个围绕<input>
元素的YUI包装对象。 You need to call setSelectionRange()
on the input itself rather than the YUI wrapper object. 您需要在输入本身而不是YUI包装器对象上调用setSelectionRange()
。
您还可以使用Y.Node.getDOMNode获得对原始DOM节点的引用 ,然后在其上调用.setSelectionRange。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.