开发者

Make jQuery dropdown toggle when click away?

开发者 https://www.devze.com 2023-03-09 15:37 出处:网络
I am using a jQuery drop down and I want the box to toggle when you click off the box $(document).ready(function () {

I am using a jQuery drop down and I want the box to toggle when you click off the box

$(document).ready(function () {
  $('li.menu_head').mouseenter(function开发者_运维知识库 () {
    $('div.newsadviceDrop').slideToggle('medium');
  });
});

Can you help me?


I had to do something similar and the way I worked it out was to do something like

$('div.newsadviceDrop').click(function (e) {
    e.stopPropagation();
});
$(document).click(function() {
    ('div.newsadviceDrop').slideToggle('medium');
});

Basically, you need to toggle the menu if any other place is clicked, but if the actual location is clicked, you need to prevent the event from bubbling up to perform a toggle.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号