簡體   English   中英

如何在文本選擇時捕獲事件

[英]How to capture event on text selection

我正在編寫一個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 );
        }
    });
});

你那里有錯字...

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

這應該是

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

現在有效嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM