Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy jQuery cannot trigger native click on an anchor tag?
    primarykey
    data
    text
    <p>Recently I found jQuery cannot trigger the native click event on an anchor tag when I'm clicking on other elements, the example below won't work:</p> <p><strong>html</strong></p> <pre><code>&lt;a class="js-a1" href="new.html" target="_blank"&gt;this is a link&lt;/a&gt; &lt;a class="js-a2" href="another.html" target="_blank"&gt;this is another link&lt;/a&gt; </code></pre> <p><strong>javascript</strong></p> <pre><code>$('.js-a1').click(function () { $('.js-a2').click(); return false; }); </code></pre> <p>And here is the <a href="http://jsfiddle.net/Witcher42/f9vkd/">jsfiddle - 1</a>. Click on the first link won't trigger native click on the second one.</p> <p>After some searches, I found a solution and an explanation.</p> <h3>Solution</h3> <p>Use the native DOM element.</p> <pre><code>$('.js-a1').click(function () { $('.js-a2').get(0).click(); return false; }); </code></pre> <p>And here is the <a href="http://jsfiddle.net/Witcher42/YCeUh/">jsfiddle - 2</a>.</p> <h3>Explanation</h3> <p>I found a post on <em>Learn jQuery</em>: <a href="http://learn.jquery.com/events/triggering-event-handlers/">Triggering Event Handlers</a>. It told me:</p> <blockquote> <p>The .trigger() function cannot be used to mimic native browser events, such as clicking on a file input box or an anchor tag. This is because, there is no event handler attached using jQuery's event system that corresponds to these events.</p> </blockquote> <h3>Question</h3> <p>So here comes my question:</p> <p>How to understand <em>'there is no event handler attached using jQuery's event system that corresponds to these events'</em>?</p> <p>Why is there not such corresponding event handler?</p> <h3>EDIT</h3> <p>I update my jsfiddles, it seems there's and error on the class name.</p>
    singulars
    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. 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