Zeb*_*bra 4 html javascript jquery dom execcommand
function selected() {
var selObj = window.getSelection();
}
Run Code Online (Sandbox Code Playgroud)
此函数返回网页中的选定文本.如何返回所选区域的html.这可能与标签<img>和<a>标签有关吗?
以下是功能列表:https://developer.mozilla.org/Special :
Tags?tag = DOM&languageUen
Tim*_*own 23
以下内容将在所有主流浏览器中执行此操作,并且与此答案完全相同:
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;
}
Run Code Online (Sandbox Code Playgroud)