我有三个包含唯一网址的文本框和按钮。单击复制按钮时,它应复制特定的文本框值,我需要与 Ctrl + v 绑定,并通过jQuery或javascript函数鼠标右键单击并粘贴事件。 / p>
当我将光标聚焦在浏览器地址栏中并且当我使用 Ctrl + v 或right click and paste go
事件时,它应该从文本框中粘贴复制的URL并转到到特定网址。
那么如何在单击复制按钮后在jQuery / javascript中绑定粘贴事件?
答案 0 :(得分:4)
检查此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!');
}
});
检查此Fiddle是否通过jQuery绑定复制,剪切和粘贴事件。
$(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>
希望这会有所帮助..
答案 1 :(得分:0)
$(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');
}
});