在jQuery中双击功能不起作用

时间:2021-11-26 21:27:18

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元素。当我在两者上调用jquery双击函数时,该函数仅在第一个元素上调用。我使用以下代码:

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

and jquery function is:

和jquery函数是:

 $("[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.

当我双击shiftTime_1的元素Id时。那么功能正常。但是,当我双击shiftTime_2的元素Id时,此函数不响应。

Please help. Thanks

请帮忙。谢谢

3 个解决方案

#1


3  

try use inside $(document).ready()

尝试在$(document).ready()中使用

$(document).ready(function(){

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

});

#2


1  

When I try the code, it works just fine:

当我尝试代码时,它工作得很好:

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

Check if there is something different from your code.

检查您的代码是否有不同之处。

#3


1  

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

如果您打算动态添加元素,请使用.on(例如,使用$(“body”)。append(“ 1 ”);)

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

#1


3  

try use inside $(document).ready()

尝试在$(document).ready()中使用

$(document).ready(function(){

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

});

#2


1  

When I try the code, it works just fine:

当我尝试代码时,它工作得很好:

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

Check if there is something different from your code.

检查您的代码是否有不同之处。

#3


1  

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

如果您打算动态添加元素,请使用.on(例如,使用$(“body”)。append(“ 1 ”);)

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