php - 要将所选文本从 div 复制到文本字段,请双击文本

标签 php javascript jquery yii

在我的应用程序中,我已经从一个目录中读取了文本文件并将其放置在一个 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();
        }
    });
};

关于php - 要将所选文本从 div 复制到文本字段,请双击文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17500236/

相关文章:

javascript - 悬停或单击运行功能

javascript - Chrome 扩展 "$ is not defined"错误

javascript - CSS 和 PHP 运行 exec 并返回原始页面

PHP 从一种表单更新数据库上的不同字段

javascript - 如何获得带有 highcharts 的响应式图表?

javascript - 获取动态创建的元素,点击javascript

php - 为什么当其他不相关的对象被序列化时,对象会变成字符串?

php - 奇怪的 anchor 标记行为

javascript - 在页面加载时本地化/翻译单词

c# - jquery mouseover() 检查拖放中的文件?