Possible Duplicate:
jQuery equivalent of JavaScript's addEventListener method
Also from a very good jQuery tutorial on : http://itunes.apple.com/in/app/designmobileweb/id486198804?mt=8
What is the jQuery equivalent for the following statement;
element1.addEventListener('click',doSomething2,false)
If it is the bind()
method, is there any option to specify the last parameter (i.e. event bubbling or capturing ... true/false)开发者_StackOverflow
Try this
// Setting the third argument to false will attach a function
// that prevents the default action from occurring and
// stops the event from bubbling.
$("#element1").bind("click", doSomething2, false);
Yes I'm pretty certain .bind() will work as you need it. Check out the jQuery .bind() docs page I'm sure you can figure out the setup. Demo code below:
$(document).ready(function() {
$("#element1").bind('click', function() {
// do something on click
}
});
use jquerys bind method
example:
document.bind("custom-listener", someCustomFunction, false);
document.trigger("custom-listener", {jsonArgsKey:jsonValue});
function someCustomFunction(json)
{
alert(json.jsonArgsKey);
}
Like this:
$(element1).click(doSomething)
If you want to stop bubbling, call event.stopPropagation()
in your doSomething
function, like this:
function doSomething (event){
event.stopPropagation()
// do whatever
}
There's no way to set up a capturing event handler with jQuery, though.
精彩评论