如何在普通 javascript 中使鼠标双击单击一次?
how to make a mouse double click on a single click in plain javascript?
嗨,我正在写一个没有 JQuery 的插件。
单击一下,item.id 未定义,link 不打开。
但是在第二次点击后,id 被定义了。
所以我正在寻找一种变通方法,使单击此 href 时鼠标双击变成单击 link。
div 没有自己的默认 ID。
感谢您的帮助
当您单击 a
具有 anchorLink
的 class 标签时,此代码会触发双击
我已经在代码中逐行解释了它,并评论了它是如何工作的
// Creating an event for double click (dblclick)
var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true
});
// get every a tag with class anchorLink and loop through them
document.querySelectorAll("a.anchorLink").forEach(anchorLink => {
// add click event to it
anchorLink.addEventListener('click', function (e) {
// trigger double click event we created at top
anchorLink.dispatchEvent(event);
});
// This is just to show that it triggers double click by single clicking
// You dont need from here
anchorLink.addEventListener('dblclick', function (e) {
console.log(e.target.innerText);
});
//to here
//
});
<a class="anchorLink" href="#" title="db2un_Fri Oct 18 16_38_15 2019.log" data-dojo-attach-point="linkColumnAnchor">db2un_Fri Oct 18 16_38_15 2019.log</a>
<br>
<a class="anchorLink" href="#" title="db2un_Fri Oct 18 16_38_15 2020.log" data-dojo-attach-point="linkColumnAnchor">db2un_Fri Oct 18 16_38_15 2020.log</a>
嗨,我正在写一个没有 JQuery 的插件。
单击一下,item.id 未定义,link 不打开。 但是在第二次点击后,id 被定义了。
所以我正在寻找一种变通方法,使单击此 href 时鼠标双击变成单击 link。
div 没有自己的默认 ID。
感谢您的帮助
当您单击 a
具有 anchorLink
的 class 标签时,此代码会触发双击
我已经在代码中逐行解释了它,并评论了它是如何工作的
// Creating an event for double click (dblclick)
var event = new MouseEvent('dblclick', {
'view': window,
'bubbles': true,
'cancelable': true
});
// get every a tag with class anchorLink and loop through them
document.querySelectorAll("a.anchorLink").forEach(anchorLink => {
// add click event to it
anchorLink.addEventListener('click', function (e) {
// trigger double click event we created at top
anchorLink.dispatchEvent(event);
});
// This is just to show that it triggers double click by single clicking
// You dont need from here
anchorLink.addEventListener('dblclick', function (e) {
console.log(e.target.innerText);
});
//to here
//
});
<a class="anchorLink" href="#" title="db2un_Fri Oct 18 16_38_15 2019.log" data-dojo-attach-point="linkColumnAnchor">db2un_Fri Oct 18 16_38_15 2019.log</a>
<br>
<a class="anchorLink" href="#" title="db2un_Fri Oct 18 16_38_15 2020.log" data-dojo-attach-point="linkColumnAnchor">db2un_Fri Oct 18 16_38_15 2020.log</a>