有没有办法将以下两条指令组合成更高效的代码?
$('.sandwiches').mouseleave(function () {
$('.sandwiches').hide();
});
$('.food').mouseleave(function () {
$('.sandwiches').hide();
});
有没有办法将以下两条指令组合成更高效的代码?
$('.sandwiches').mouseleave(function () {
$('.sandwiches').hide();
});
$('.food').mouseleave(function () {
$('.sandwiches').hide();
});
通过组合选择器:
$('.sandwiches,.food').mouseleave...
$('.sandwiches, .food').mouseleave(function () {
$('.sandwiches').hide();
});
您可以将定义的函数附加到每个元素:
function hideElement(e) {
$(this).hide();
}
$('.sandwiches,.food').mouseleave(hideElement);