How to raise Jquery Events and handle them?
I have a page with a widget in it. There is no cross scripting. I want to use Jquery events to communicate between the two. How can I raise a jquery event on a div in widget and then catch it in widget and do开发者_JAVA百科 something with it?
Any examples or code snippets would be really helpful.
If you can get by with the standard events then this is very simple. You bind an event handler to the div in the widget (assume the div has id="widget"
) thus:
$('#widget').bind('click', function(e){
// your code here runs when widget div receives the click event.
// e is the W3-standard DOM event, in case you need it, but
// you're free to ignore it
});
then you can trigger that from elsewhere with $('#widget").click()
. It would be neat to define custom events to do this but that's a bit more advanced. Once you have the basics working you can google "jquery custom events".
Take a look at this question, even if it's not exactly the same it might help you
精彩评论