admin管理员组文章数量:1022525
Here is my question:
When the user makes a selection in an article or in the editing area of a WYSWYG editor widget, the selection can span over multiple elements, like anchors, images, span tags... even block-level elements (but no table in my problem).
I know how to retrieve a Range object from the selection,
but could not find a reliable solution to get the content text of the Range
object.
I'm not looking for a solution for IE (its TextRange object has a .text
property).
Thanks!
Here is my question:
When the user makes a selection in an article or in the editing area of a WYSWYG editor widget, the selection can span over multiple elements, like anchors, images, span tags... even block-level elements (but no table in my problem).
I know how to retrieve a Range object from the selection,
but could not find a reliable solution to get the content text of the Range
object.
I'm not looking for a solution for IE (its TextRange object has a .text
property).
Thanks!
Share Improve this question edited Jun 24, 2011 at 18:05 Luc125 asked Jun 24, 2011 at 18:00 Luc125Luc125 5,86735 silver badges35 bronze badges2 Answers
Reset to default 4Have you looked at the quirksmode article on Range?
Based on this article, you could create a method like this:
function getRangeText() {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
} else if (document.selection) {
userSelection = document.selection.createRange();
}
var selectedText = userSelection;
if (userSelection.text) {
selectedText = userSelection.text;
}
return selectedText;
}
I tested this in FF5, Opera 11, Safari on the Mac, as well as IE6 and IE7. It's worth testing in the other IE browsers, but my guess is it works in them, as well.
This returns a string and works in all major browsers:
function getSelectionText() {
var text = ""
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}
Here is my question:
When the user makes a selection in an article or in the editing area of a WYSWYG editor widget, the selection can span over multiple elements, like anchors, images, span tags... even block-level elements (but no table in my problem).
I know how to retrieve a Range object from the selection,
but could not find a reliable solution to get the content text of the Range
object.
I'm not looking for a solution for IE (its TextRange object has a .text
property).
Thanks!
Here is my question:
When the user makes a selection in an article or in the editing area of a WYSWYG editor widget, the selection can span over multiple elements, like anchors, images, span tags... even block-level elements (but no table in my problem).
I know how to retrieve a Range object from the selection,
but could not find a reliable solution to get the content text of the Range
object.
I'm not looking for a solution for IE (its TextRange object has a .text
property).
Thanks!
Share Improve this question edited Jun 24, 2011 at 18:05 Luc125 asked Jun 24, 2011 at 18:00 Luc125Luc125 5,86735 silver badges35 bronze badges2 Answers
Reset to default 4Have you looked at the quirksmode article on Range?
Based on this article, you could create a method like this:
function getRangeText() {
var userSelection;
if (window.getSelection) {
userSelection = window.getSelection();
} else if (document.selection) {
userSelection = document.selection.createRange();
}
var selectedText = userSelection;
if (userSelection.text) {
selectedText = userSelection.text;
}
return selectedText;
}
I tested this in FF5, Opera 11, Safari on the Mac, as well as IE6 and IE7. It's worth testing in the other IE browsers, but my guess is it works in them, as well.
This returns a string and works in all major browsers:
function getSelectionText() {
var text = ""
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type == "Text") {
text = document.selection.createRange().text;
}
return text;
}
本文标签: javascriptHow to get the text of a selection over multiple HTML elementsStack Overflow
版权声明:本文标题:javascript - How to get the text of a selection over multiple HTML elements? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745522981a2154400.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论