如何使用Javascript在一个页面上分别使用两个复制事件?
我使用“复制”addEventListener一页。而且,在单击按钮时,我也在使用“复制”,但问题是,当我使用“单击复制内容”时,默认情况下,复制事件也会触发。当我在单击“复制”时所需的内容,则默认情况下,应删除或隐藏该页面上的副本 代码如下:如何使用Javascript在一个页面上分别使用两个复制事件?,javascript,Javascript,我使用“复制”addEventListener一页。而且,在单击按钮时,我也在使用“复制”,但问题是,当我使用“单击复制内容”时,默认情况下,复制事件也会触发。当我在单击“复制”时所需的内容,则默认情况下,应删除或隐藏该页面上的副本 代码如下: document.addEventListener('copy', (event) => { const pagelink = `\n${document.getElementById('judgmentNameHighlight').valu
document.addEventListener('copy', (event) => {
const pagelink = `\n${document.getElementById('judgmentNameHighlight').value}`;
event.clipboardData.setData('text', document.getSelection() + pagelink);
event.preventDefault();
});
这个是默认的,并且
$("#btnCitationParenthesisCopy").click(function () {
var judgmentNameHighlight = $("#judgmentNameHighlight").val();
var courtName = $('#courtNameCustom').val();
var url = window.location.pathname;
var urlid = url.substring(url.lastIndexOf('/') + 1);
$.ajax({
type:'GET',
url:'/getCitations',
data:{urlid:urlid},
success:function(data){
var finaldata = `${judgmentNameHighlight}, ${data['ac']} ("These two appeals, one by the convict, ${data['res']} and the other by the ${data['pes']} are directed against one and the same judgment of the ${courtName} and were therefore heard together and are disposed of this common judgment.")`;
$('#copyCitation').val(finaldata);
var copyText = document.getElementById("copyCitation");
copyText.select();
copyText.setSelectionRange(0, 99999)
document.execCommand("copy");
// alert("Copied the text: " + copyText.value);
}
});
});
当我从这段代码中复制任何内容时,第一段代码也默认运行
使用“单击复制事件”时,如何删除默认复制事件
任何帮助都将不胜感激
谢谢尝试使用。。我没有检查
window.globalFlag = false;
在你点击事件调用它
element.on('click', function() {
window.globalFlag = true;
// after ajax success and copy call it back
window.globalFlag = false;
});
在你复制事件中
document.addEventListener('copy', (event) => {
if (!window.globalFlag) {
// do you staff here
}
});
使用全局布尔标志并在两个事件中分配它OK谢谢你能详细说明这将很容易理解。我应该在代码中更改什么?现在我的复制事件不起作用了。单击工作正常。在
单击
事件中执行复制后,请尝试再次将窗口设置为false
。我只需更新我的答案@firatozcevahir谢谢我忘记将变量设置回false。