【问题标题】:How to bind paste event in keyboard and mouse?如何在键盘和鼠标中绑定粘贴事件?
【发布时间】:2015-08-10 16:38:25
【问题描述】:

我有三个文本框和按钮,其中包含一个唯一的 URL。单击复制按钮时,它应该复制特定的文本框值,我需要使用 Ctrl+v 和鼠标右键单击并通过 jQuery 或 javascript 函数粘贴事件。

当我将光标聚焦在浏览器地址栏中并且当我使用 Ctrl+vright click and paste go 事件时,它应该从文本框中粘贴复制的 url 并转到特定网址。

那么如何在点击复制按钮后在 jQuery/javascript 中绑定粘贴事件呢?

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

查看FIDDLE,了解如何在输入和文本区域中执行此操作。支持鼠标和键盘事件。

HTML:

<p>
  <input class="js-copytextinput" value="http://www.stackoverflow.com"></input>
  <button class="js-textinputcopybtn">Copy Text Input</button>
</p>

<p>
    <textarea class="js-copytextarea">http://www.stackexchange.com</textarea>
  <button class="js-textareacopybtn">Copy Textarea</button>
</p>

JS:

//textinput copy
    var copyTextinputBtn = document.querySelector('.js-textinputcopybtn');

    copyTextinputBtn.addEventListener('click', function(event) {
        var copyTextinput = document.querySelector('.js-copytextinput');
        copyTextinput.select();

        try {
            var successful = document.execCommand('copy');
            var msg = successful ? 'successful' : 'unsuccessful';
            console.log('Copying text input command was ' + msg);
        } catch (err) {
            console.log('Oops, unable to copy');
        }
    });

来源:Snippet from the answer provided by Dean Taylor with little modifications

你可以像这样在jQuery中绑定复制粘贴和剪切事件,

$(".select").bind({
    copy : function(){
        $('span').text('copy behaviour detected!');
    },
    paste : function(){
        $('span').text('paste behaviour detected!');
    },
    cut : function(){
        $('span').text('cut behaviour detected!');
    }
});

在通过 jQuery 绑定复制、剪切和粘贴事件时检查此 Fiddle

  • 键和鼠标事件都绑定在剪切、复制和粘贴中。

$(document).ready(function() {
  //textinput copy
  var copyTextinputBtn = document.querySelector('.js-textinputcopybtn');

  copyTextinputBtn.addEventListener('click', function(event) {
    var copyTextinput = document.querySelector('.js-copytextinput');
    copyTextinput.select();

    try {
      var successful = document.execCommand('copy');
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text input command was ' + msg);
    } catch (err) {
      console.log('Oops, unable to copy');
    }
  });

  //textarea copy
  var copyTextareaBtn = document.querySelector('.js-textareacopybtn');

  copyTextareaBtn.addEventListener('click', function(event) {
    var copyTextarea = document.querySelector('.js-copytextarea');
    copyTextarea.select();

    try {
      var successful = document.execCommand('copy');
      var msg = successful ? 'successful' : 'unsuccessful';
      console.log('Copying text area command was ' + msg);
    } catch (err) {
      console.log('Oops, unable to copy');
    }
  });

});
http://www.stackoverflow.comhttp://www.stackexchange.comhttp://www.stackoverflow.comhttp://www.stackexchange.com
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>
  <input class="js-copytextinput" value="http://www.stackoverflow.com"></input>
  <button class="js-textinputcopybtn">Copy Text Input</button>
</p>

<p>
  <textarea class="js-copytextarea">http://www.stackexchange.com</textarea>
  <button class="js-textareacopybtn">Copy Textarea</button>
</p>

希望这会有所帮助..

【讨论】:

  • 感谢您的回答,但我想要的是,当单击复制按钮复制包含一个唯一 URL 的文本时,当光标聚焦在浏览器地址栏中时,我想通过 ctrl 将复制 URL 粘贴到浏览器地址栏中+v 和鼠标事件。
  • @RaviKavaiya 在我的回答中试试 jsfiddle。您可以使用 jquery 绑定事件捕获的所有事件。鼠标和键盘动作都被记录下来。 ;)
【解决方案2】:
$(document).ready(function() {
    $("#editor").bind('paste', function (e){
        $(e.target).keyup(getInput);
    });

    function getInput(e){
        var inputText = $(e.target).html();
        alert(inputText);
        $(e.target).unbind('keyup');
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 2017-08-15
    • 1970-01-01
    相关资源
    最近更新 更多