小编典典

如何在contenteditable元素中将所选文本替换为html?[重复]

javascript

如何使用contenteditable元素将所选内容替换为自己的html?


阅读 330

收藏
2020-05-01

共1个答案

小编典典

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;
}
}
alert(html);
}

2020-05-01