最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Triggering click on a link doesn't change location hash - Stack Overflow

programmeradmin3浏览0评论

I'm working on a web application that uses onHashChange event listener in some situations and manually clicking on a link with href="#hash" works perfectly well. But when I trigger click on the same link using jQuery's $('a[href=#"hash"]').trigger('click') or $('a[href=#"hash"]').click() hash in address bar is not changing.

Is it something that I'm doing wrong? or I shoud use another method for this purpose?

HTML

<a href="#hash">Do Something</a>

JS

// Not working
$('a[href="#hash"]').click();

// Not working
$('a[href="#hash"]').trigger('click');

I'm working on a web application that uses onHashChange event listener in some situations and manually clicking on a link with href="#hash" works perfectly well. But when I trigger click on the same link using jQuery's $('a[href=#"hash"]').trigger('click') or $('a[href=#"hash"]').click() hash in address bar is not changing.

Is it something that I'm doing wrong? or I shoud use another method for this purpose?

HTML

<a href="#hash">Do Something</a>

JS

// Not working
$('a[href="#hash"]').click();

// Not working
$('a[href="#hash"]').trigger('click');
Share Improve this question asked Jan 3, 2017 at 15:53 Farid RnFarid Rn 3,2076 gold badges41 silver badges66 bronze badges 6
  • Can you demonstrate this using a fiddle? – Anubhav Commented Jan 3, 2017 at 15:58
  • Do you preventDefault() in your click handler? – AndFisher Commented Jan 3, 2017 at 16:00
  • 1 Silly Question. Why trigger a click on the link? Why not just handle it all programatically? – allnods Commented Jan 3, 2017 at 16:09
  • @allnods Imagine a page that when loads with a url hash does something based on url's hash. and changing hash does the same thing. I want to create a default hash that if user opened the page without any hash in url, I simulate clicking on a default button. Sorry if my question doesn't describe the situation very well; but beleive me it's not as silly as you might think it is! – Farid Rn Commented Jan 3, 2017 at 16:18
  • @FaridRn - I have used this method before, but rather than going through the link, just set 'window.location'... Lot simpler. – allnods Commented Jan 3, 2017 at 16:24
 |  Show 1 more ment

3 Answers 3

Reset to default 5

New guy here hopefully not making an azz of himself. I just thought maybe something in this code I'm using on my site might help you. It kinda seems similar to what you're describing.

$(document).ready(function(){
  $('a[href^="#"]').on('click',function (e) {
    e.preventDefault();

    var target = this.hash;
    var $target = $(target);

    $('html, body').stop().animate({
      'scrollTop': $target.offset().top
    }, 900, 'swing', function () {
      window.location.hash = target;
    });
  });
   });

What you wrote is true (it's enough to debug jQuery source code): the trigger click event doesn't work on an anchor.

In order to achieve what you are trying you can get the dom element and then fire the click:

$('a[href="#hash"]').get(0).click()

This only will work.

The click method (when used by jquery) triggers the click events that you register using the el.click(function.. and el.on('click', function...

You can create a new MouseEvent and dispatch it directly to the relevant element:

e = document.createEvent('MouseEvents');
e.initEvent("click", true, true);
$('a[href="#hash"]')[0].dispatchEvent(e)

The above code will work in Chrome, Firefox and IE

Or just use the click event of the element (which will not use jquery's click function, but the browser's click function):

$('a[href="#hash"]')[0].click()

Note that this code might not work in several browsers due to security reasons.

发布评论

评论列表(0)

  1. 暂无评论