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

javascript - How do I simulate user clicking a link in JQuery? - Stack Overflow

programmeradmin0浏览0评论

Now there's quite a lot of similar-looking questions here, but I was wondering, what should I do, if I want not to just change the location of the window, but trigger whatever functions may be bound to the click event, and only change the href if they are ok with it, or just redirect, if there are no listeners.

For example:

var a = $('.edithost');
a.click(function() {return false;});

Should I click the link with mouse, it never takes me to the href, so just redirecting user to attr('href') would change the intended behavior of the page. Besides, clicking applies not only to links, but to, say, buttons too, in which case I would have to submit the form, etc.

So I was wondering, whether it is possible to emulate clicking an element, so that all the behavior of the browser is exactly the same, as if clicked with mouse?

There can be no listeners bound to a link.

Example:

var a = $('<a href="google">google</a>');
a.click();
a.trigger('click');

This won't take you to Google, I want to make it do.

Update: .click() won't work. trigger('click') too. preventDefault has nothing to do with this

Now there's quite a lot of similar-looking questions here, but I was wondering, what should I do, if I want not to just change the location of the window, but trigger whatever functions may be bound to the click event, and only change the href if they are ok with it, or just redirect, if there are no listeners.

For example:

var a = $('.edithost');
a.click(function() {return false;});

Should I click the link with mouse, it never takes me to the href, so just redirecting user to attr('href') would change the intended behavior of the page. Besides, clicking applies not only to links, but to, say, buttons too, in which case I would have to submit the form, etc.

So I was wondering, whether it is possible to emulate clicking an element, so that all the behavior of the browser is exactly the same, as if clicked with mouse?

There can be no listeners bound to a link.

Example:

var a = $('<a href="google.com">google</a>');
a.click();
a.trigger('click');

This won't take you to Google, I want to make it do.

Update: .click() won't work. trigger('click') too. preventDefault has nothing to do with this

Share Improve this question edited Nov 28, 2011 at 11:10 Fluffy asked Nov 28, 2011 at 10:51 FluffyFluffy 28.4k42 gold badges156 silver badges238 bronze badges 2
  • stackoverflow.com/questions/809038/… when you .click() then use .PreventDefault() – Niranjan Singh Commented Nov 28, 2011 at 11:08
  • Did you try live() jquery function? api.jquery.com/live – wasimbhalli Commented Nov 28, 2011 at 11:15
Add a comment  | 

4 Answers 4

Reset to default 16

In IE, you can call .click() on the element, but for standard browsers you need to simulate the click event by creating a native mouse event and then use dispatchEvent to trigger it.

I summed it all up in this jQuery 'plugin':

$.fn.simulateClick = function() {
    return this.each(function() {
        if('createEvent' in document) {
            var doc = this.ownerDocument,
                evt = doc.createEvent('MouseEvents');
            evt.initMouseEvent('click', true, true, doc.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
            this.dispatchEvent(evt);
        } else {
            this.click(); // IE
        }
    });
}

Now, just call:

$('.edithost').simulateClick();

Try a[0].click();

This will execute the click method of the DOM element instead of triggering the event.

Yes, very simple:

$('#YourSelector').click();

this will emulate a user click.

I don't really get what you want to do. with this :

a.click(function(e){
e.preventDefault();
});

You can stop the default behaviour of the event (in this case, redirecting to a.attr('href')). You can then perform whatever task you want and then redirect the user manually.

Is this what you're looking for ?

If you want to trigger the click event, just do

a.click();
发布评论

评论列表(0)

  1. 暂无评论