gpt4 book ai didi

javascript - 多个元素的相同点击事件,具体情况

转载 作者:行者123 更新时间:2023-12-03 06:11:56 28 4
gpt4 key购买 nike

我不想重复我的代码,所以我试图弄清楚如何在多个元素上使用单击事件:

$(document).ready(function() {
var realStateBtn = $('#real-estate-center').parent();
var link = $('#real-estate-center > .linkwidth');
realStateBtn.css('zIndex', 9999);
realStateBtn.click(function() {
console.log('TRIGGERED');
window.location.href='http://realestatecenter.bankofamerica.com/';
});

link.click(function() {
window.location.href="http://realestatecenter.bankofamerica.com/";
});
});

正如您所看到的,变量 realStateBtnlink 将您带到相同的位置/应用了相同的功能,那么我该怎么做才能将相同的代码放入一个函数?

我知道我可以做这样的事情:

$('.class1, class2').click(...)

但在本例中我有以下元素:

$('#real-estate-center').parent();

$('#real-estate-center > .linkwidth');

建议?

最佳答案

您可以使用.add()组合 jQuery 对象的方法。

realStatebtn.add(link).click(function() {
console.log('TRIGGERED');
window.location.href='http://realestatecenter.bankofamerica.com/';
}

关于javascript - 多个元素的相同点击事件,具体情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39277106/

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