2014-01-09 38 views
0

我寫一個JavaScript腳本來執行的文本選擇的任何行動:如何在文本選擇捕捉事件

HTML

<div class="text"> 
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem 
Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of 
type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release 
of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus 
PageMaker including versions of Lorem Ipsum. 
</div> 

的JavaScript

//Trigger event when mouse down on html element having class ".text" 
$(".text").on("mousedown", function() { 
    //trigger event when mouse up either with text selection or blank 
    $(this).one("mouseup", function() {   
     //get selected text 
     var $textSelection = window.getSelection().toString(); 
     //check if text selected or not 
     if ($textSelection.length > 0){ 
      //alert($textSelection.length) 
      alert("You have selected: "+$textSelection); 
     } 
    }); 
}); 
+1

它工作正常,是什麼問題? –

+1

順便說一句,有什麼問題?請參閱:http://jsfiddle.net/2FF93/ –

+0

也許你忘記把你的代碼放在document.ready –

回答

0

你有錯字有...

$(this).one("mouseup", function() { 

這應該是

$(this).on("mouseup", function() { 

它現在的工作?

+0

夥計們,我沒有錯過任何東西。我用一個代替了。一個在jQuery 1.7中引入,與.on()相同。如果我錯了,請讓我說得對 –