代码之家  ›  专栏  ›  技术社区  ›  Silver Light

jquery:选择文本时运行代码

  •  1
  • Silver Light  · 技术社区  · 14 年前

    我有一个这样的HTML文档:

    <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有一个 .select() event ,这将使此操作相当容易,但似乎当所选文本位于输入或文本区域内时,它可以工作。

    以下是我尝试过的代码:

    $(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');
                }
            );
    
        }
    );
    

    什么都没有发生。

    有没有办法解决这个问题?

    1 回复  |  直到 13 年前
        1
  •  4
  •   Nick Craver    14 年前

    jquery核心没有任何功能,但是 some plugin options .

    您的代码如下所示:

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

    You can test it out here .