Javascript createTextRange(); 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14641379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript createTextRange(); issue
提问by Madura Harshana
After excuting alert, next line gives an error.how can I fix this using javascript or jquery?
执行警报后,下一行出现错误。如何使用 javascript 或 jquery 解决此问题?
for(var nI = 0; nI < aOrderNumList.length; nI++) {
if(!isEmpty(aOrderNumList[nI])) {
alert("Invalid Order Number");
var oTextRange = $("#OrderNumList").createTextRange();
var lFound = oTextRange.indexOf(aOrderNumList[nI])!=-1;
if(lFound) {
oTextRange.select();
}
return false;
}
}
HTML code
HTML代码
<tr>
<td>Order Number List:</td>
<td><textarea tabindex="<%=nIndex+1%>" id="OrderNumList" name="OrderNumList" rows="2" cols="35" <%=VClass("OrderNumList","")%>></textarea></td>
</tr>
Thank you very much.
非常感谢你。
采纳答案by Chinthaka Rupasinghe
As in your code, you need to select text content of the input element (text, textarea type input fields).
在您的代码中,您需要选择输入元素的文本内容(文本、textarea 类型的输入字段)。
Java-Script
Java脚本
document.getElementById("selector").select()
//selector is element id
JQuery
查询
setTimeout(function () {
$("#selector").select();
}, 1);
//selector is element id, also can you with class selector ie. $(".selector").select();
回答by phnkha
I think it should be:
我觉得应该是:
var oTextRange = $("#OrderNumList")[0].createTextRange();
createTextRange is a method of DOM object, not jquery object.
createTextRange 是 DOM 对象的方法,而不是 jquery 对象。
Update:
更新:
The createTextRange method is supported by the body, button, textarea and the input elements, but the use of the method raises an exception for some input elements (checkbox, image, radio). The isTextEdit property can be used to avoid the exception.
正文、按钮、文本区域和输入元素都支持 createTextRange 方法,但是使用该方法会引发某些输入元素(复选框、图像、收音机)的异常。isTextEdit 属性可用于避免异常。
see more: http://help.dottoro.com/ljfahrpo.php
回答by CBarr
I echo what everyone else said, but do keep in mind that if this is the onlything you are doing, you don't event need jQuery.
我赞同其他人所说的,但请记住,如果这是您唯一要做的事情,则不需要 jQuery。
var oTextRange = document.getElementById("OrderNumList").createTextRange();