2008-12-28 132 views

回答

2

使用OnClick

:select仅适用于某些表单元素。

6

你可以通过使用onMouseUp事件来实现你想要的。见下面...

<html> 
    <body> 
     <div id="container" style="border: 1px solid #333" contentEditable="true" onMouseUp="checkMe()">Type text here</div> 

    </body> 
    <script language="javascript"> 
    function checkMe() { 
     var txt = ""; 

     if (window.getSelection) { 
       txt = window.getSelection(); 
     } 
     else if (document.getSelection) { 
      txt = document.getSelection(); 
     } else if (document.selection) { 
      txt = document.selection.createRange().text; 
     } 

     alert("Selected text is " + txt); 
    } 
    </script> 
</html> 
+1

这或多或少是我正在寻找,但它不考虑用户将使用键盘来扩展或移动选择的可能性。但是,将相同的逻辑绑定到onKeyUp可能会起作用。 – 2014-10-27 03:55:50