javascript jQuery 中的双击功能不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9680290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Double click function in jQuery is not working
提问by manishjangir
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
请帮忙。谢谢
采纳答案by Kanishka Panamaldeniya
try use inside $(document).ready()
尝试在里面使用 $(document).ready()
$(document).ready(function(){
$("[id^='shiftTime_']").dblclick(function() {
alert("hello");
});
});
回答by Igor L.
Use .onif you plan to add elements dynamically (e.g. by using $( "body" ).append( "<span id='shiftTime_2'>1</span>" );
)
如果您打算动态添加元素,请使用.on(例如,通过使用$( "body" ).append( "<span id='shiftTime_2'>1</span>" );
)
$( "body" ).on( "dblclick", "span", function() {
alert( "This works also with dynamically added elements" );
} );
回答by Guffa
When I try the code, it works just fine:
当我尝试代码时,它工作得很好:
http://jsfiddle.net/Guffa/pfQfK/
http://jsfiddle.net/Guffa/pfQfK/
Check if there is something different from your code.
检查是否有与您的代码不同的内容。