2010-10-17 49 views
1

我有一個像這樣llokes的HTML文檔:Jquery:選擇文本時運行代碼

<div class="this_can_be_selected">Some text here</div> 
<div>No text in the div above is selected</div> 

<div class="this_can_be_selected">Some text here</div> 
<div>No text in the div above is selected</div> 

<div class="this_can_be_selected">Some text here</div> 
<div>No text in the div above is selected</div> 

我希望將文本No text in the div above is selected更改爲Some text in the div above is selected時,猜猜看,上面div中的一些文本被選中了:)

jQuery has一個.select() event,這將使這很容易做,但似乎,當選定的文本在輸入或textarea內時,它工作。

這是我嘗試過的代碼:

$(document).ready(function() 
    { 

     //Some text inside div is selected 
     $('DIV.this_can_be_selected').select 
     (
      function() 
      { 
       alert('Something is selected!'); 
       $(this).next().html('Some text in the div above is selected'); 
      } 
     ); 

    } 
); 

什麼都沒發生。

有沒有辦法解決這個問題?

回答

4

jQuery核心沒有這個東西,但有some plugin options

您的代碼應該是這樣的:

$(function() { 
    $(document).bind('textselect', function(e) { 
    alert('Selected Text: ' + e.text); 
    });  
}); 

You can test it out here

+0

謝謝!它做到了! – 2010-10-17 12:24:16