2012-02-15 8 views
0
<div id="container" style="border: 1px solid #333" contentEditable="true">Type text here</div> 

テキストを選択するとトリガーする機能はありますか? (Type text heredivでイベントを選択する方法は?

+1

が重複する可能性を[することができます私は、テキストまたはテキストエリアの外のテキストの選択からJavascriptイベントを取得しますか?](http://stackoverflow.com/questions/1791526/can-i-get-a-javascript-event-from-the-selection-of-text -outside-of-text-or-texta) –

+0

私のソリューションをチェックしてください...あなたの問題は解決しました.... –

+0

@FahimParkar:そうではありません。選択は、キーボードまたは編集およびコンテキストメニューによってもトリガされます。 –

答えて

2

イベントは、onMouseUpイベントを使用して実現できます。あなたがイベントを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> 
+0

また、編集やコンテキストメニューからキーボード選択と「すべて選択」があります。 –

関連する問題