gpt4 book ai didi

javascript - 如何通过单击外部并调用关闭事件的单击函数来关闭展开的 div

转载 作者:行者123 更新时间:2023-12-02 14:55:30 25 4
gpt4 key购买 nike

嗯,这里有很多此类问题的解决方案。但是,我的问题是我不允许编辑现有功能。现有功能是:

$('body').on('click', '.parent.normal', function () {
// code for Expanding a div
});

$('body').on('click', '.parent.expand', function () {
// code for Closing expanded div
});

我能做的是定义另一个 click 函数,用于在展开的 div 外部单击,该函数将调用现有的 click 事件来关闭展开的 div。为此,我写了这样的内容:

  if($('.parent.expand').length > 0) {
$('div:not(".parent.normal, .expanded-content, .expanded-content > div")').click(function () {
$('.parent.expand').click();
});
}

这实际上不起作用。我怎样才能让它工作?

Fiddle Demo

最佳答案

将此添加到您现有的代码中必须解决该问题。

$(document).on('click',function(e){   
if (!$(e.target).parents('.content').length > 0){
$('.parent.expand').click();
}
});

这是一个working Fiddle

关于javascript - 如何通过单击外部并调用关闭事件的单击函数来关闭展开的 div,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35848854/

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