簡體   English   中英

要將所選文本從div復制到文本字段,請雙擊文本

[英]To copy the selected Text from a div to a text field on double click the Text

在我的應用程序中,我已從目錄中讀取文本文件並將其放在div中,如下所述

     $pageText = fread($fh, 25000); ?>
     <div id="click">Hai
    <?php  echo nl2br($pageText);
      ?> </div>

現在我所做的是,點擊div它將div中的整個文本復制到文本字段,這是我的javascript,它完美地復制整個div但現在我需要的是我想要復制只有雙擊時從div到文本字段的選定文本

      <script type="text/javascript">
        $(document).ready( function() {
        $('#click').click(function() { 
        $("#txtMessage").insertAtCaret($(this).text());
        return false
         });

        });

       $.fn.insertAtCaret = function (myValue) {
       return this.each(function(){
       //IE support
       if (document.selection) {
       this.focus();
       sel = document.selection.createRange();
       sel.text = myValue;
       this.focus();
       }
        //MOZILLA / NETSCAPE support
        else if (this.selectionStart || this.selectionStart == '0') {
        var startPos = this.selectionStart;
        var endPos = this.selectionEnd;
        var scrollTop = this.scrollTop;
        this.value = this.value.substring(0, startPos)+           myValue+this.value.substring(endPos,this.value.length);            
this.focus();
this.selectionStart = startPos + myValue.length;
this.selectionEnd = startPos + myValue.length;
this.scrollTop = scrollTop;
       } else {
      this.value += myValue;
      this.focus();
      }
      });
      };
      </script>

以下是雙擊獲取所選文本的方法,

更新:現在它也復制到最后的焦點輸入。

首先,您需要在雙擊文本之前將焦點設置為其中一個輸入。

演示http://jsfiddle.net/yeyene/GYuBv/15/

$(document).ready(function () {
    $("input[type=text]").focus(function(){
        $("input[type=text]").removeClass('lastFocus');
        $(this).addClass('lastFocus');
    });

    $('#myDiv').dblclick(function() {  
        $('#selected').append(getSelectionText());
        $("input[type=text].lastFocus").val($("input[type=text].lastFocus").val()+getSelectionText());        
    });
});

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

$.fn.insertAtCaret = function (myValue) {
    return this.each(function () {
        if (document.selection) {
            this.focus();
            sel = document.selection.createRange();
            sel.text = myValue;
            this.focus();
        }
        else if (this.selectionStart || this.selectionStart == '0') {
            var startPos = this.selectionStart;
            var endPos = this.selectionEnd;
            var scrollTop = this.scrollTop;
            this.value = this.value.substring(0, startPos) + myValue + this.value.substring(endPos, this.value.length);
            this.focus();
            this.selectionStart = startPos + myValue.length;
            this.selectionEnd = startPos + myValue.length;
            this.scrollTop = scrollTop;
        } else {
            this.value += myValue;
            this.focus();
        }
    });
};

嘗試這個:

$('#click').dblclick(function() {
    var text = '';
    if (window.ActiveXObject) {
        text = document.selection.createRange().htmlText;
    } else {
        text = getSelection().getRangeAt(0);
    }
    $("#txtMessage").insertAtCaret(text);
    return false;
});

暫無
暫無

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

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