Javascript 如何使用“获取HTML”;window.getSelection();
我的代码:Javascript 如何使用“获取HTML”;window.getSelection();,javascript,internet-explorer,google-chrome,w3c,Javascript,Internet Explorer,Google Chrome,W3c,我的代码: <p id="p"> 123<span>abc</span>456 </p> <script> document.getElementById("p").onmouseup=function(){ if (window.getSelection) { //code } else if (document.selection) {
<p id="p">
123<span>abc</span>456
</p>
<script>
document.getElementById("p").onmouseup=function(){
if (window.getSelection) {
//code
}
else if (document.selection) {
alert(document.selection.createRange().htmlText) //IE6 7 8
}
}
</script>
123abc456
document.getElementById(“p”).onmouseup=function(){
if(window.getSelection){
//代码
}
else if(document.selection){
警报(document.selection.createRange().htmlText)//IE6 7 8
}
}
在“//code”中写些什么,我可以在chrome或FF中获得相同的htmlText?
这是一个精确的复制品。请关闭.alert(this.outerHTML);而不是警报(selObj);
document.getElementsByTagName('p')[0].onmouseup = function() {
if (typeof window.getSelection === 'function') {
//code
var selObj = window.getSelection();
alert(selObj);
var selRange = selObj.getRangeAt(0);
} else if (document.selection) {
alert(document.selection.createRange().htmlText) //IE6 7 8
}
}