【发布时间】:2011-08-04 08:35:46
【问题描述】:
我可以使用以下代码来获取选定的文本:
text=window.getSelection(); /// for Firefox
text=document.selection.createRange().text; /// for IE
但是我怎样才能获得选定的 Html,其中包括 text 和 html 标签?
【问题讨论】:
-
Rangy 现在是on GitHub。
我可以使用以下代码来获取选定的文本:
text=window.getSelection(); /// for Firefox
text=document.selection.createRange().text; /// for IE
但是我怎样才能获得选定的 Html,其中包括 text 和 html 标签?
【问题讨论】:
在 IE
document.selection.createRange().htmlText
正如@DarrenMB 指出的 IE11 不再支持这一点。参考this answer。
在非 IE 浏览器中,我只是尝试玩这个...这似乎可行,会产生将节点分成两半并创建额外跨度的副作用,但这是一个起点:
var range = window.getSelection().getRangeAt(0),
content = range.extractContents(),
span = document.createElement('SPAN');
span.appendChild(content);
var htmlContent = span.innerHTML;
range.insertNode(span);
alert(htmlContent);
不幸的是,我似乎无法将节点恢复原状(例如,您可以从跨度中提取一半的文本)。
【讨论】:
cloneContents()而不是extractContents()
我发现highlight 插件是最好的搭配,它非常轻巧,您可以使用它突出显示部分内容:
$('li').highlight('bla');
【讨论】:
@zyklus:
我修改了你的函数来工作(我使用的是 jQuery,但这些部分可以很容易地用 Javascript 重写):
function getSelectionHtml() {
var htmlContent = ''
// IE
if ($.browser.msie) {
htmlContent = document.selection.createRange().htmlText;
} else {
var range = window.getSelection().getRangeAt(0);
var content = range.cloneContents();
$('body').append('<span id="selection_html_placeholder"></span>');
var placeholder = document.getElementById('selection_html_placeholder');
placeholder.appendChild(content);
htmlContent = placeholder.innerHTML;
$('#selection_html_placeholder').remove();
}
return htmlContent;
}
【讨论】:
这就是我想出的。通过 IE、Chrome、Firefox、Safari、Opera 测试。不返回空字符串。
function getSelected() {
var text = "";
if (window.getSelection
&& window.getSelection().toString()
&& $(window.getSelection()).attr('type') != "Caret") {
text = window.getSelection();
return text;
}
else if (document.getSelection
&& document.getSelection().toString()
&& $(document.getSelection()).attr('type') != "Caret") {
text = document.getSelection();
return text;
}
else {
var selection = document.selection && document.selection.createRange();
if (!(typeof selection === "undefined")
&& selection.text
&& selection.text.toString()) {
text = selection.text;
return text;
}
}
return false;
}
【讨论】:
这是一个函数,可以让您获得与所有主要浏览器中当前选择相对应的 HTML。它还可以处理选择中的多个范围(目前仅在 Firefox 中实现):
function getSelectionHtml() {
var html = "";
if (typeof window.getSelection != "undefined") {
var sel = window.getSelection();
if (sel.rangeCount) {
var container = document.createElement("div");
for (var i = 0, len = sel.rangeCount; i < len; ++i) {
container.appendChild(sel.getRangeAt(i).cloneContents());
}
html = container.innerHTML;
}
} else if (typeof document.selection != "undefined") {
if (document.selection.type == "Text") {
html = document.selection.createRange().htmlText;
}
}
return html;
}
alert(getSelectionHtml());
【讨论】:
textarea.value.slice(textarea.selectionEnd),IE
first paragraph TEXT TO
BE SELECTED second paragraph
的大写锁定部分时,您不会得到预期的“TEXT TOBE SELECTED”但“
TEXT TO
BE SELECTED
- 由于某种原因浏览器广告标签不在选择中的顺序关闭那些。有什么方法可以只获得选择中的真实内容吗?getSelectionHtml() 的结果包括额外的换行符——除此之外,这是一个很好的修复!
.createElement 是否会导致内存泄漏...我们不应该使用container.remove(); 进行清理,否则无论如何都会被GC 收集?跨度>