简体   繁体   English

如何获取 JQuery.trigger('click'); 启动鼠标单击

[英]How to get JQuery.trigger('click'); to initiate a mouse click

I'm having a hard time understand how to simulate a mouse click using JQuery.我很难理解如何使用 JQuery 模拟鼠标点击。 Can someone please inform me as to what i'm doing wrong.有人可以告诉我我做错了什么。

HTML: HTML:

<a id="bar" href="http://stackoverflow.com" target="_blank">Don't click me!</a>
<span id="foo">Click me!</span>

jQuery: jQuery:

jQuery('#foo').on('click', function(){
    jQuery('#bar').trigger('click');
});

Demo: FIDDLE演示:小提琴

when I click on button #foo I want to simulate a click on #bar however when I attempt this, nothing happens.当我点击按钮 #foo 我想模拟点击 #bar 但是当我尝试这样做时,什么也没有发生。 I also tried jQuery(document).ready(function(){...}) but without success.我也试过jQuery(document).ready(function(){...})但没有成功。

You need to use jQuery('#bar')[0].click();你需要使用jQuery('#bar')[0].click(); to simulate a mouse click on the actual DOM element (not the jQuery object), instead of using the .trigger() jQuery method. 模拟鼠标点击实际 DOM 元素(不是 jQuery 对象),而不是使用.trigger() jQuery 方法。

Note: DOM Level 2 .click() doesn't work on some elements in Safari .注意:DOM Level 2 .click()不适用于 Safari 中的某些元素 You will need to use a workaround.您将需要使用一种解决方法。

http://api.jquery.com/click/ http://api.jquery.com/click/

您只需要在执行.click()之前放置一个小的超时事件,如下所示:

setTimeout(function(){ $('#btn').click()}, 100);

This is JQuery behavior.这是 JQuery 行为。 I'm not sure why it works this way, it only triggers the onClick function on the link.我不确定为什么它会这样工作,它只会触发链接上的 onClick 函数。

Try:尝试:

jQuery(document).ready(function() {
    jQuery('#foo').on('click', function() {
        jQuery('#bar')[0].click();
    });
});

See my demo: http://jsfiddle.net/8AVau/1/看我的演示: http : //jsfiddle.net/8AVau/1/

jQuery(document).ready(function(){
    jQuery('#foo').on('click', function(){
         jQuery('#bar').simulateClick('click');
    });
});

jQuery.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 Boss!
        }
    });
}

May be useful:可能有用:

The code that calls the Trigger should go after the event is called.调用触发器的代码应该调用事件之后

For example, I have some code that I want to be executed when #expense_tickets value is changed, and also, when page is reload例如,我有一些代码希望在 #expense_tickets 值更改时执行,并且在重新加载页面时执行

$(function() { 

  $("#expense_tickets").change(function() {
    // code that I want to be executed when #expense_tickets value is changed, and also, when page is reload
  });

  // now we trigger the change event
  $("#expense_tickets").trigger("change");

})

jQuery's .trigger('click'); jQuery 的.trigger('click'); will only cause an event to trigger on this event, it will not trigger the default browser action as well.只会导致在此事件上触发事件,也不会触发默认浏览器操作。

You can simulate the same functionality with the following JavaScript:您可以使用以下 JavaScript 模拟相同的功能:

jQuery('#foo').on('click', function(){
    var bar = jQuery('#bar');
    var href = bar.attr('href');
    if(bar.attr("target") === "_blank")
    {
        window.open(href);
    }else{
        window.location = href;
    }
});

试试这个对我有用:

$('#bar').mousedown();

I have tried top two answers, it doesn't worked for me until I removed "display:none" from my file input elements.我已经尝试了前两个答案,直到我从我的文件输入元素中删除了“display:none”,它才对我有用。 Then I reverted back to .trigger() it also worked at safari for windows.然后我恢复到 .trigger() 它也适用于 Windows 的 safari。

So conclusion, Don't use display:none;所以结论,不要使用 display:none; to hide your file input , you may use opacity:0 instead.要隐藏您的文件输入,您可以使用 opacity:0 代替。

Technically not an answer to this, but a good use of the accepted answer ( https://stackoverflow.com/a/20928975/82028 ) to create next and prev buttons for the tabs on jQuery ACF fields:从技术上讲,这不是一个答案,而是很好地利用已接受的答案 ( https://stackoverflow.com/a/20928975/82028 ) 为 jQuery ACF 字段上的选项卡创建下一个和上一个按钮:

$('.next').click(function () {
    $('#primary li.active').next().find('.acf-tab-button')[0].click();
});

$('.prev').click(function () {
    $('#primary li.active').prev().find('.acf-tab-button')[0].click();
});

Just use this:只需使用这个:

$(function() {
 $('#watchButton').trigger('click');
});

You can't simulate a click event with javascript.您无法使用 javascript 模拟点击事件。 jQuery .trigger() function only fires an event named "click" on the element, which you can capture with .on() jQuery method. jQuery .trigger()函数仅在元素上触发名为“click”的事件,您可以使用.on() jQuery 方法捕获该事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM