日期:2014-05-16 浏览次数:20347 次
采用JavaScript得到当前用户选中文本,有一定的浏览器局限性!
IE:document.selection FireFox:window.getSelection() 或有其他浏览器
?
<html>
<head>
<title>Readme</title>
?<script type="text/javascript">
???function getSelectedText(){
????var selectedText ="";
????if(window.getSelection) {??
?????????? selectedText = window.getSelection().getRangeAt(0);??
?????? }??
?????? else if(document.selection && document.selection.createRange) {??
?????????? selectedText = document.selection.createRange().text;
?????? }
???alert(selectedText);
???var item= document.getElementById("bodyID");
???var range= item.createTextRange();
???alert(range.text)
????}
?</script>
</head>
<body id="bodyID">
<h1>Readme</h1>
<b>version 5.2.1</b>
<b>released on 2008-12-29</b>
<input type="button" vlaue="getSelectedText" name="getSelectedText" onclick="getSelectedText()"/>
</body>
</html>