Combining selectors?

2019-03-01 02:35发布

I have two click events as follows:

$($('[id^="tab"]')).live('click', function() {
    alert($(this).attr("id"));
});

$($('[id^="home"]')).live('click', function() {
    alert($(this).attr("id"));
});

Is it possible to combine this into one click even as the only difference is "home" and "tab"?

3条回答
唯我独甜
2楼-- · 2019-03-01 03:01
$('[id^="home"], [id^="tab"]')
查看更多
劫难
3楼-- · 2019-03-01 03:05

Sure, simply divide them by ,:

$('[id^="tab"], [id^="home"]').live('click', function() {
    alert($(this).attr("id"));
});
查看更多
Deceive 欺骗
4楼-- · 2019-03-01 03:11
$('[id^="tab"], [id^="home"]').live('click', function() {
    alert($(this).attr("id"));
});

http://jsfiddle.net/yUYVh/3/

查看更多
登录 后发表回答