[jQuery] why is the "click" event different in <a> elements?
Imagine this code:
$("a").click(function () { alert("hello world"); return false; });
$("button").click(function () { $("a").click(); });
I would expect that clicking on the button would trigger the click
event on the links element, but it is not the case because of the
following code in jQuery:
// Handle triggering native .onfoo handlers (and on links since we
don't call .click() for links)
if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) &&
elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
val = false;
// some code in between these blocks
// Trigger the native events (except for clicks on links)
if ( fn && donative !== false && val !== false && !(jQuery.nodeName
(elem, 'a') && type == "click") ) {
this.triggered = true;
try {
elem[ type ]();
// prevent IE from throwing an error for some hidden elements
} catch (e) {}
}
It is around line 2019 in jquery-1.2.6.js (the trigger function).
So my question is: why jQuery makes an exception with the <a>
elements? At least it should mention this in the docs. I spent quite
some time until I found this.
Best regards