开发者

Two events at the same time

开发者 https://www.devze.com 2023-03-17 14:17 出处:网络
I want to do some action开发者_Python百科s when class .smth is clicked $(\'.smth\').click(function() {

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");
});
0

精彩评论

暂无评论...
验证码 换一张
取 消