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) {
return container.innerHTML;
}
else if (win.document.getSelection) {
return container.innerHTML;
}
else if (win.document.selection) {
return win.document.selection.createRange().htmlText;
}
你这样写的,所以就 有时候 innerHTML,有时候 htmlTex()
上全代码: htmlTex 或者做个demo,出来,根据你的的我只能这么说了,很难判断
demojava太外行了,htmlText写错2次,win.getSelection居然想不到是window.getSelection,还是先自己好好学学吧