Two events at the same time
I want to do some action开发者_Python百科s when class .smth is clicked
$('.smth').click(function() {
and when key is pressed:
$('.txt').bind('keypress', function(e) {
I want to do the same action, so how can I use them both with OR or something like that?
$('.log').click.or.$('.txt').bind('keypress', function(e) {
?
THank you.
If this was the same collection of elements you could use:
$(".myclass").bind("click keypress", function(event) {
//...
});
But as it's different elements you'll have to follow Felix advice and write a function then attach it as the event handler.
Use a named function instead of an anonymous one.
function handler() {
//...
}
$('.txt').keypress(handler);
$('.smth').click(handler);
you can handle multiple things in one event have a look on this
$(".smth, .txt, .log").bind("click keypress", function(event) {
console.log("key pressed");
});
精彩评论