Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To stop the event bubbling up to parent elements you have to tell the <code>event</code> object about it. In IE, you set <code>event.cancelBubble= true</code>. In other browsers, you call <code>event.stopPropagation()</code>.</p> <p>You probably also want to turn off the default link-following action so that the browser doesn't keep jumping up to the top trying to follow the non-existing anchor links like <code>#1</code>. In IE, you set <code>event.returnValue= false</code>. In other browsers, you call <code>event.preventDefault()</code>.</p> <p>The <code>event</code> object is accessible as <code>window.event</code> on IE. On other browsers, it is passed into the event handler function. A way to pass the event into a function that works on both is:</p> <pre><code> &lt;li onclick="nada('2.1', event);"&gt;&lt;a href="#2.1"&gt;2.1&lt;/a&gt;&lt;/li&gt; function nada(n, event) { alert(n); if ('stopPropagation' in event) { event.stopPropagation(); event.preventDefault(); } else { event.cancelBubble= true; event.returnValue= false; } } </code></pre> <p>However it would probably be better all round to put the <code>onclick</code> event on the <code>a</code> element which it usually belongs. This helps for accessibility, as the <code>a</code> element will be focusable and keyboard-operable. And it means you don't have to worry about parents' click handlers being called.</p> <p>(You can style the <code>a</code> to look like a plain block, if you want.)</p> <p>You can then also kick out the redundant <code>onclick</code> links with a bit of unobtrusive scripting:</p> <pre><code>&lt;ul id="nadalist"&gt; &lt;li&gt;&lt;a href="#1"&gt;1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#2"&gt;2&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#2.1"&gt;2.1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#2.2"&gt;2.2&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#2.3"&gt;2.3&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li&gt;&lt;a href="#4"&gt;4&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#5"&gt;5&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;script type="text/javascript"&gt; var links= document.getElementById('nadalist').getElementsByTagName('a'); for (var i= links.length; i--&gt;0;) { links[i].onclick= function() { alert(this.hash.substring(1)); return false; } } &lt;/script&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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