Hi Tom,
I'm not sure what exactly you're trying to achieve because the logo does
already have a link back to the home page manually added via the <a> tag
wrapping the logo (e.g. href="/").
And running jQuery('#header a:first').click() won't do anything (as far as
I'm aware) because all you are doing is binding a click event 'listener' to
that <a> element but not specifying a 'handler' to take over when the event
actually happens.
Instead you could do something like...
jQuery('#header a:first').click(function(e){ console.log(e);
e.preventDefault(); })
...which means when the user clicks on the logo the console will log the
jQuery event object. And you could swap that out for some other
functionality you want to have happen.
And with regards to your second example jQuery('#header ul a:first').click(),
this works because if you look at the element in WebKit's 'Elements' tab
you'll see that this particular <a> element has a parent <li> with a click
event attached to it, so the user is clicking on the <a> but also having the
event bubble up to the <li> and triggering the click event attached to the
<li> as well.
Hopefully I've not made a mistake there, but that looks to be what's
happening.
Kind regards,
Mark
On 18 February 2011 12:20, Tom <[email protected]> wrote:
> I wouldn't normally come to a list like this for what seems like a
> basic issue (and have a feeling I am about to embarrass my self by
> having asked something very silly), but this one really has me
> stumped.
>
> jQuery('#header a:first').click() should, all things being equal,
> simulate a click event on the first anchor element in <div
> id="header">
>
> So, go to http://www.autoquake.com/ and put the above code in your
> console and see what happens. It *should* reload the homepage.
>
> For me, it doesn't. It returns the jQuery object with the located
> element and does nothing.
>
> I've tried it in various browsers (Chrome, Firefox, IE9) and multiple
> PCs & networks and it just doesn't work.
>
> Bizarrely, doing the same thing on other links on the page does work:
>
> eg: jQuery('#header ul a:first').click()
>
> It also appears that it's not just the first link of the page. I found
> a couple of other instances of this where deep links don't respond.
>
> Also, it doesn't appear to be due to any other code running on the
> page. For example, I built this:
>
> <!DOCTYPE html>
> <html>
> <head>
> <script type="text/javascript"
> src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js
> "></script>
> </head>
> <body>
> <a href="http://www.google.com" id="clickMe">click me</a>
> </body>
> </html>
>
> (Available at http://simplebutgood.net/test-page.html)
>
> jQuery('a').click() does not work.
>
> I then tried an event trigger routine in Chrome:
>
> var element = document.getElementsById('clickMe');
> var evt = document.createEvent("HTMLEvents");
> evt.initEvent(event, true, true ); // event type,bubbling,cancelable
> return !element.dispatchEvent(evt);
>
> This works fine.
>
> So... what is jQuery doing wrong?
>
> Thanks,
> Tom H.
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]