Double click function in jQuery is not working

喜你入骨 提交于 2019-12-22 08:20:16

问题


I have two span elements in a page. when I call a jquery double click function on both then the function is called only on first element. I am using the following code:

<span id="shiftTime_1">1</span>
<span id="shiftTime_2">1</span>

and jquery function is:

 $("[id^='shiftTime_']").dblclick(function() {
 alert("hello");
 });

when I double click on the element Id of shiftTime_1. then the function works fine. But when I double click on element Id of shiftTime_2 then this function does not respond.

Please help. Thanks


回答1:


try use inside $(document).ready()

$(document).ready(function(){

 $("[id^='shiftTime_']").dblclick(function() {
 alert("hello");
 });

});



回答2:


Use .on if you plan to add elements dynamically (e.g. by using $( "body" ).append( "<span id='shiftTime_2'>1</span>" ); )

$( "body" ).on( "dblclick", "span", function() {
    alert( "This works also with dynamically added elements" );
} );



回答3:


When I try the code, it works just fine:

http://jsfiddle.net/Guffa/pfQfK/

Check if there is something different from your code.



来源:https://stackoverflow.com/questions/9680290/double-click-function-in-jquery-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!