gpt4 book ai didi

javascript - 如何让onclick();收听多个按钮

转载 作者:行者123 更新时间:2023-11-30 11:52:41 25 4
gpt4 key购买 nike

我试图让 Jq 在同一个 onclick 方法中监听三个按钮然后触发一个函数,通过$(this)调用被点击的按钮;

这是一个示例:

$("body").on('click', 'a.home:visible', 'a.mobile:visible', 'a.phone:visible', function () {
var attr = $(this).attr('attr');


$(this).parents('.dropdown-menu').prev().prev().text(attr);
});

最佳答案

你做的基本上是正确的。你的方法很好。但是你必须将它组合在一个字符串中,而不是作为单个参数。而且您不需要:visible,因为您不能单击 不可见元素。 ;)

$("body").on('click', 'a.home, a.mobile, a.phone', function() {
var attr = $(this).attr('attr');
$(this).parents('.dropdown-menu').prev().prev().text(attr);
});

如果元素是静态的,您甚至应该使用普通的事件监听器而不是委托(delegate)。

$('a.home, a.mobile, a.phone').click(function() {
var attr = $(this).attr('attr');
$(this).parents('.dropdown-menu').prev().prev().text(attr);
});

关于javascript - 如何让onclick();收听多个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38991248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com