js如何实时精确地获取鼠标选中的html内容,代码如下:
functiongetHtml(win){if(win.getSelection){varrange=win.getSelection().getRangeAt(0);v...
function getHtml(win){
if (win.getSelection) {
var range=win.getSelection().getRangeAt(0);
var container = win.document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
}
else if (win.document.getSelection) {
var range=win.getSelection().getRangeAt(0);
var container = win.document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
}
else if (win.document.selection) {
return win.document.selection.createRange().htmlText;
}
}
这段代码有时可以获取到带样式的html,但有时获取的却是纯文本,不知道是什么原因,我想精确地获取选中的html,不知道该如何获取? 展开
if (win.getSelection) {
var range=win.getSelection().getRangeAt(0);
var container = win.document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
}
else if (win.document.getSelection) {
var range=win.getSelection().getRangeAt(0);
var container = win.document.createElement('div');
container.appendChild(range.cloneContents());
return container.innerHTML;
}
else if (win.document.selection) {
return win.document.selection.createRange().htmlText;
}
}
这段代码有时可以获取到带样式的html,但有时获取的却是纯文本,不知道是什么原因,我想精确地获取选中的html,不知道该如何获取? 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询