function selected() {
var selObj = window.getSelection();
}
该函数返回网页中选定的文本。如何返回所选区域的HTML?是否可以使用
<img>
和<a>
标签实现?以下是函数列表:
https://developer.mozilla.org/Special:Tags?tag=DOM&language=en
function selected() {
var selObj = window.getSelection();
}
<img>
和<a>
标签实现?以下代码适用于所有主流浏览器,并且是此答案的完全副本:
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;
}