javascript document.selection.createRange() 在 chrome 和 safari 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7021497/
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
document.selection.createRange() not working in chrome and safari
提问by Mathew Paul
I am using a textbox with textmode as multiline option its working fine in IE,Mozilla problem occurs in Chrome and safari. Sample code folows
我正在使用带有 textmode 作为多行选项的文本框,它在 IE 中工作正常,在 Chrome 和 safari 中出现 Mozilla 问题。示例代码如下
<asp:TextBox ID="txtRootDescription" onpaste="DoPaste(this);" Width="600px" Rows="10" Columns="72" MaxLength="500" TextMode="MultiLine" runat="server"></asp:TextBox>
function DoPaste(control) {
maxLength = control.attributes["maxlength"].value;
if (maxLength) {
var oTR = control.document.selection.createRange();
}}
in chrome it gives me an error as "Cannot read property 'selection' of undefined"
在 chrome 中,它给了我一个错误“无法读取未定义的属性‘选择’”
回答by Mrchief
In non IE (excluding IE9)browsers (see comments), use window.getSelection
to get the selection object. In IE < 9, original code should work.
在非 IE(不包括 IE9)浏览器中(见注释),用于window.getSelection
获取选择对象。在 IE < 9 中,原始代码应该可以工作。
function GetSelection () {
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;
}
}
}
function DoPaste(control) {
maxLength = control.attributes["maxlength"].value;
if (maxLength) {
var oTR = GetSelection();
}
}
In general, working with selection
and ranges
is very tricky as browser supports varies so much.
一般来说,使用selection
和ranges
是非常棘手的,因为浏览器支持变化很大。
Here's an excellent reference which lists out browser support (and what code works where) and sample code that works in corresponding browsers: http://help.dottoro.com/ljefwsqm.php
这是一个很好的参考,它列出了浏览器支持(以及代码在何处工作)和在相应浏览器中工作的示例代码:http: //help.dottoro.com/ljefwsqm.php
回答by RobG
There are a number of foibles when getting selected text in a document, mostly related to whether or not text is selected in a form control or as text of some other element. Try a function that does something like:
在文档中获取选定文本时有许多缺点,主要与是否在表单控件中选择文本或作为其他元素的文本有关。尝试执行以下操作的函数:
function checkForSelectedText(e) {
var e = e || window.event;
var el = e.target || e.srcElement;
var tagName = el.tagName && el.tagName.toLowerCase();
var t;
var d = document;
// Try DOM 2 Range - for most browsers, including IE 6+
// However, doesn't get text selected inside form controls
// that allow selection of text content (input type text,
// textarea)
if (d && d.selection && d.selection.createRange) {
t = d.selection.createRange().text;
// Otherwise try HTML5 - note that getSelection returns
// a string with extra properties. This may also get
// text within input and textarea
} else if (d.getSelection) {
t = d.getSelection();
}
// If didn't get any text, see if event was inside
// inupt@type=text or textarea and look for text
if (t == '') {
if (tagName == 'textarea' ||
(tagName == 'input' && el.type == 'text')) {
// Check selectionStart/End as otherwise if no text
// selected, IE returns entire text of element
if (typeof el.selectionStart == 'number' &&
el.selectionStart != el.selectionEnd) {
t = el.value.substring(el.selectionStart, el.selectionEnd)
}
}
}
return t;
}
回答by Davide Piras
I would use JQuery to do the same because it's cross browser and you write more abstract java script instead of the native browser specific one your wrote so far.
我会使用 JQuery 来做同样的事情,因为它是跨浏览器的,并且您编写了更抽象的 Java 脚本,而不是您目前编写的特定于本机浏览器的脚本。