日期:2014-05-16 浏览次数:20352 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
?<HEAD>
? <TITLE> New Document </TITLE>
? <META NAME="Generator" CONTENT="EditPlus">
? <META NAME="Author" CONTENT="ZhangYi">
? <META NAME="Keywords" CONTENT="">
? <META NAME="Description" CONTENT="">
? <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
? <style type="text/css">
???
? </style>?
?</HEAD>
?<BODY>
??? <input type="text" onfocus="this.select()" id="txt"/>
???
??? <input type="button" value="获取文本框内的值" id="btn"/>
??? <script type="text/javascript">
??????? //Firefox, Safari, Opera下,可以用window.getSelection(), 参考MDC
??????? //IE下,可以用document.selection.createRange().text, 参考MSDN放在一起:
??????? //注意:当选中的是input[type=text]里面的值时getSelection在Firefox和Opera下无法获取到选取值
??????? //,在Safari下没问题。
??????? function getSelectionText() {
??????????? if(window.getSelection) {
??????????? return window.getSelection().toString();
??????????? } else if(document.selection && document.selection.createRange) {
??????????? return document.selection.createRange().text;
??????????? }
??????? return '';
??????? }
???????
??????? document.getElementById("btn").onclick = function() {
??????????? alert(getSelectionText())
??????? }
??? </script>
?</BODY>
</HTML>