jQuery Dropdown Menu Functionality based on Click Events (hiding help) -


I have two jQuery menus that are displayed properly when clicking on "show". For example, we say you have two links:

"Show 1", "Show 2"

You click "Show 1" and then click on a div in " Show 1 content appears with "

You click on" Show 2 "and then a Divide appears with" Show 2 content "

I found that point Has worked on

Obviously there are some useful things that I need to deal if I click on "Show 1" and then click on "Show 2" then I will "Show the contents of 1" disappear Another thing, if I click anywhere on the page, whatever dropdown is active, I want to hide it to click outside the content box.

My DOM structure:

  ul li.menu span = link_to '1 show' ul.dropdown.hidden li = link_to 'show1 content' li.menu span = Link_to 'show2' ul.dropdown.hidden li = link_to 'show2 content'  

my JS:

  $ ("# search li.menu Span a "). Click (function (event) {event.preventDefault (); $ (this) .Parent (s). Sister ("Ul.dropdown"). ToggleClass ("hidden");});  

So basically I need to understand how to hide when clicking outside of the ul.dropdown box and how to hide when clicking on the other ul.dropdown box

Thank you.

You have to pair the click event with document and target (event delegation) Check if the goal is outside NAV, hide the dropdown and return.

Example:

  & lt; Ul id = "nav" & gt; & Lt; Li class = "menu" & gt; & Lt; Span & gt; & Lt; A href = "#" & gt; Show 1 & lt; / A & gt; & Lt; / Span & gt; & Lt; Ul class = "dropdown" & gt; & Lt; Li & gt; & Lt; A href = "#" & gt; Show 1 content & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; Li class = "menu" & gt; & Lt; Span & gt; & Lt; A href = "#" & gt; Show 2 & lt; / A & gt; & Lt; / Span & gt; & Lt; Ul class = "dropdown" & gt; & Lt; Li & gt; & Lt; A href = "#" & gt; 2 Show content & lt; / A & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; / Li & gt; & Lt; / Ul & gt; & Lt; Script & gt; Var nav = $ ('# nav'); . Hide Nav.find ('ul.dropdown'); $ (Document) .bind ('click', function (e) {var target = $ (e.target); if (target.closest ('# nav'). Length & lt; 1) {nav.find (' Ul ('span')) (var li = target.closest ('li.menu')); li.siblings (). Search () 'Ul.dropdown'). Hide (); Li.find ('ul.dropdown'). Toggle (); E.preventDefault ();}}) & lt; / Script & gt;  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -