function inadvertently executed when using .live in jQuery
I'm trying to bind a click event handler to some ele开发者_JAVA技巧ments that are being created dynamically. But the function already gets executed on simply loading the page. I also tried the livequery plugin and .delegate which also had that unwanted habit.
$(".pika_thumb").live("click" ,( function () {
$("#video").hide();
$(".pika_main").show();
}));
How do I prevent my function to be executed on other events than a click on the specified elements?
The only obvious error is the brackets from around the function() { }
definition.
However that wouldn't cause immediate invocation unless you had trailing a ()
parameter list too. Is your code snippet complete?
the syntax:
$(".pika_thumb").live("click" , function () {
$("#video").hide();
$(".pika_main").show();
});
check if you don't have something else that fires the click on your .pika_thumb class.
Aside from the brackets, my final working code is:
$("#gallery").delegate(".pika_thumb", "mousedown" , function () {
$(".video-js").get(0).pause();
$("#video").hide();
$(".pika_main").show();
});
Thanks everyone!
精彩评论