Jquery-一个.click事件中包含两个变量

Jquery - Two variables in one .click event

本文关键字:包含两 变量 事件 一个 click Jquery-      更新时间:2023-09-26

我的变量中有多个函数。然后我想在.click事件中调用这些变量。一个很好,但我想要两个,甚至更多。我该怎么做?下面是我期望工作的代码。

var hideServices = function() {
            jQuery(".services-inner").css({"opacity": "0"});
            jQuery(".insignia-inner").css({"opacity": "0"});
            jQuery(".insignia-inner-text").css({"opacity": "0"});
};
var showMilitaryKit = function() {
            jQuery(".military-kit-inner").css({"opacity": "1"});
};
var showProperty = function() {
            jQuery(".property-kit-inner").css({"opacity": "1"});
};
    jQuery(".military-kit-hover").click(hideServices, showMilitaryKit);
    jQuery(".property-hover").click(hideServices, showProperty);

我确信我没有在最后一行的.click事件中正确地组合我的变量,但我找不到任何关于我想要实现的目标的文档。有人有适合我的调整吗?

将两个调用封装在一个匿名函数中:

jQuery('.military-kit-hover').click(function() {
    hideServices();
    showMilitaryKit();
});

如果需要保留事件对象或this,请执行以下操作:

jQuery('.military-kit-hover').click(function(e) {
    hideServices.call(this, e);
    showMilitaryKit.call(this, e);
});
jQuery(".military-kit-hover").click(function() {
    hideServices();
    showMilitaryKit();
});

JQuery.click()文档中的更多信息。