[英]createRange function is not working in other browsers except IE
I have a text box. 我有一个文本框。 When I write first text , then java script function will fire to check whether it is a number.
当我写第一个文本时,然后将触发Java脚本函数以检查它是否为数字。
For IE , the following code is returning -1 value, But Other browsers it does not alert any value. 对于IE,以下代码返回-1值,但其他浏览器不会警告任何值。 Please help me
请帮我
var selected = document.selection.createRange();
var decInSel = selected.text.indexOf('.');
hasNegInSel = selected.text.indexOf('-') != -1;
alert(decInSel);
document.selection.createRange()
is IE < 9 only ... you the following function for cross browser support document.selection.createRange()
仅限于IE <9 ...您可以使用以下函数来支持跨浏览器
function getSelectedText() {
if (window.getSelection) { // all browsers, except IE before version 9
var selectionRange = window.getSelection ();
return selectionRange.toString();
} else {
if (document.selection.type == 'None') {
return "";
} else {
var textRange = document.selection.createRange();
return textRange.text;
}
}
}
So your full code would look like this : 因此,您的完整代码如下所示:
var selected = getSelectedText()
var decInSel = selected.indexOf('.');
hasNegInSel = selected.indexOf('-') != -1;
alert(decInSel);
Use 采用
window.getSelection()
window.getSelection()
Does chrome supports document.selection? chrome是否支持document.selection?
Other browsers use selectionStart
and selectionEnd
properties of the text box. 其他浏览器使用文本框的
selectionStart
和selectionEnd
属性。 You can get the selected text in a text box in those browsers using the following: 您可以使用以下方法在这些浏览器的文本框中获取选定的文本:
var input = document.getElementById("textBox");
var selectedText = input.value.slice(input.selectionStart, input.selectionEnd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.