Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think what you are looking for is probably event.stopPropagation()</p> <p>Here's the documentation: <a href="http://api.jquery.com/event.stopPropagation/" rel="noreferrer">http://api.jquery.com/event.stopPropagation/</a></p> <p>Basically, what's happening is that when you click on the <code>&lt;a&gt;</code>, the click event is triggering, but since it is contained in the <code>&lt;li&gt;</code> the click event is also triggered on it. This process of the event going from child to parent is called event bubbling. You can use event.stopPropagation() to stop it.</p> <p>Let's assume you have an HTML structure like so:</p> <pre><code>&lt;ul&gt; &lt;li class="reveal"&gt;One&lt;br /&gt; &lt;a href="http://www.google.com" target="_blank"&gt;Google&lt;/a&gt; &lt;ul&gt; &lt;li&gt;a&lt;/li&gt; &lt;li&gt;b&lt;/li&gt; &lt;li&gt;c&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="reveal"&gt;Two&lt;br /&gt; &lt;a href="http://www.google.com" target="_blank"&gt;Google&lt;/a&gt; &lt;ul&gt; &lt;li&gt;a&lt;/li&gt; &lt;li&gt;b&lt;/li&gt; &lt;li&gt;c&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="reveal"&gt;Three&lt;br /&gt; &lt;a href="http://www.google.com" target="_blank"&gt;Google&lt;/a&gt; &lt;ul&gt; &lt;li&gt;a&lt;/li&gt; &lt;li&gt;b&lt;/li&gt; &lt;li&gt;c&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;/ul&gt; </code></pre> <p>Further, we'll say that you have a CSS rule:</p> <pre><code>ul ul { display: none; } </code></pre> <p>Apply this jQuery, and you should get the result you're looking for:</p> <pre><code>$(function () { $('.reveal').click(function() { $(this).children('ul').slideToggle(); }); $('.reveal a').click(function(event) { event.stopPropagation(); }); }); </code></pre> <p>Here's a live demo of this in action: <a href="http://jsfiddle.net/PaxTg/" rel="noreferrer">http://jsfiddle.net/PaxTg/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload