8.文本选择window.getSelection
例 1.8(文本选择IEFF.html)
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script type="text/javascript">
function fun1(){
if(window.getSelection){//DOM,FF,Webkit,Chrome,IE10
alert("马克-to-win:火狐click事件包括鼠标点下和抬起两个动作,不抬起不算结束,所以可以选择文本。 window.getSelection is "+window.getSelection);
selectionText = window.getSelection();
}else if(document.getSelection){//IE10
selectionText = document.getSelection();
}else if(document.selection){//IE6+10-
selectionText = document.selection.createRange().text;
}else{
selectionText = "";
}
strlen = selectionText.toString();
alert(strlen);
// alert(document.selection.createRange().text);
}
</script>
<body>
<body onclick="fun1();">
<p>Select this textsfsadfsdafsdagfsadgsagsadgfsd</p>
sdgsagsadgsafghgfhfh
gsadgsaghfghfhgfhf
sabhhfghgfhfghfh
gsfdghf
更多请见:http://www.mark-to-win.com/tutorial/js_5_windowgetSelection.html
本文深入探讨了不同浏览器中实现文本选择的方法,包括window.getSelection、document.getSelection及document.selection,适用于DOM、Firefox、Webkit、Chrome、IE等环境。通过示例代码展示了如何获取用户所选文本并进行处理。

1512

被折叠的 条评论
为什么被折叠?



