简体   繁体   English

双击jQuery

[英]Double Click with jQuery

I'm trying to make my webpage alert text when an element has been double-clicked using jQuery. 我正在尝试使用jQuery双击某个元素时使我的网页警报文本。 Here's my JS: 这是我的JS:

$("#dashboard-acrs-map").dblclick(function () {

    alert("asdasdasdasd")

})

And HTML: 和HTML:

<div class="dashboard-acrs-map-div" id="dashboard-acrs-map"></div>

Am I doing something wrong? 难道我做错了什么? Because the alert doesn't come up... 因为警报没有出现...

Note: It seems to be working UNLESS the element (#dashboard-acrs-map) is created using JS. 注意:除非使用JS创建元素(#dashboard-acrs-map),否则似乎可以正常工作。 How can I fix this? 我怎样才能解决这个问题?

That pretty much has to be your selected element not existing. 几乎必须是您选择的元素不存在。 It is possible that the script runs before the element is loaded. 脚本可能在元素加载之前运行。 Place the script at the end of the body for example to make sure this is not the case. 例如,将脚本放在body的末尾以确保不是这种情况。

 $("#dashboard-acrs-map").dblclick(function () { alert("asdasdasdasd") }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="dashboard-acrs-map"> Element </div> 

Dynamic element creation example: 动态元素创建示例:

 document.querySelector("button").onclick = () => { var element = document.createElement("div"); element.id = "double-click-me"; // Who cares though? element.textContent = "Double Click Me"; element.ondblclick = () => alert("Double!"); // Hook up double click here. document.body.appendChild(element); }; 
 <button>Create Double-Clickable</button> 

(Furthermore, i believe that jQuery should never be used.) (此外,我认为永远不要使用jQuery。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM