JQuery,里邊想要獲取html內(nèi)容需要提供標(biāo)簽或id或class,并不能解決該問題。
if (window.getSelection) { //現(xiàn)代瀏覽器
userSelection = window.getSelection();
var range = userSelection.getRangeAt(0);
var rangeText = range.toString();//選中文本
var div = document.createElement('div');
div.appendChild(range.cloneContents);
alert(div.innerHTML);
var rangeHtmlText = div.innerHTML;//選中內(nèi)容,包含標(biāo)簽
//如果想獲取里邊的標(biāo)簽,可以通過
//var aArr = div.getElementsByTagName('a');
//for(var i=0; i
} else if (document.selection) { //IE瀏覽器 考慮到Opera,應(yīng)該放在后面
userSelection = document.selection.createRange();
var text = userSelection.text;//獲得文本
var htmlText = userSelection.htmlText;//獲得包含標(biāo)簽的內(nèi)容
}
上面兩種方法應(yīng)該夠用了