jQuery $('a').trigger('click'); 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17105015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:26:44  来源:igfitidea点击:

$('a').trigger('click'); not working

javascriptjquery

提问by Hello-World

How do I make jquery click <a href="test.zip" id="mylink">test</a>

我如何让 jquery 点击 <a href="test.zip" id="mylink">test</a>

<script>
    // this wont work
    $('#mylink').trigger('click'); 
</script>

Please can you help

请你能帮忙吗

采纳答案by maximkou

$(document).ready(function(){
    $('#mylink').trigger('click');
});

回答by nnnnnn

If your intention is to navigate to the specified URL as if the user had clicked the link try calling the DOM .click()method instead of the jQuery .click()method:

如果您打算像用户单击链接一样导航到指定的 URL,请尝试调用 DOM.click()方法而不是 jQuery.click()方法:

$('#mylink')[0].click();

The jQuery .click()will call event handlers that you've bound but not cause the default click behaviour.

jQuery.click()将调用您已绑定但不会导致默认单击行为的事件处理程序。

回答by Broxzier

You need to trigger the default clickmethod, not the one by jQuery. This can be done by adding the default click option within a click event of jQuery using this.

您需要触发默认click方法,而不是 jQuery。这可以通过在 jQuery 的单击事件中使用this.

<a href="http://about.com/"></a>

This is how the JavaScript looks. It basically creates the event when the DOM is ready, and clicks it intermediately, thus following the link.

这就是 JavaScript 的样子。它基本上在 DOM 准备好时创建事件,并在中间单击它,从而跟随链接。

$(function() {
    $('a').click(function() {
        // 'this' is not a jQuery object, so it will use
        // the default click() function
        this.click();
    }).click();
});

To see a live example (opening about.com), see: http://jsfiddle.net/8H9UX/

要查看现场示例(打开 about.com),请参阅:http: //jsfiddle.net/8H9UX/

回答by Emil A.

Just click:

只需点击:

$("#mylink").click();

If your scripts are in the head then you need to ensure that the element exists, so the script should be executed when document is ready.

如果您的脚本在头部,那么您需要确保该元素存在,因此应该在文档准备好时执行脚本。

$(document).ready(function () {
    $("#mylink").click();
});

回答by Arun P Johny

If you are expecting the file to get downloaded, it will not happen becauer trigger()will not trigger the default event.

如果您希望下载文件,则不会发生,因为trigger()不会触发默认事件。

回答by Swarne27

use the following way.... since you want to download the file prevent the link from navigating.

使用以下方式.... 因为你想下载文件阻止链接导航。

$(document).ready(function() {
    $('#mylink').click(function(e) {
       e.preventDefault();  //stop the browser navigating
       window.location.href = 'test.zip';
    });
}); 

回答by Ripa Saha

your above code will not work because you have assigned value in hrefand then you want some operation onclickof this anchor tag.

您上面的代码将不起作用,因为您已在其中分配了值href,然后您想要onclick对该锚标记进行一些操作。

<a href="test.zip" id="mylink">test</a>

first of all you have assigned .zipto href. So it will open zip file onclick first and will not trigger any other operation in onclicktrigger.

首先,您已分配.zip给 href。所以它会先打开 zip 文件 onclick 并且不会在onclicktrigger 中触发任何其他操作。

so whatever operation you want to do , perform it first then open .zip

所以无论你想做什么操作,先执行然后打开 .zip

use code like below

使用如下代码

<a href="javascript:void(0);" id="mylink">test</a>

$('#mylink').click(function(){
    // do your operation here
    // now open zip 
});

回答by leepowell

You need to wait until the DOM has finished loading. This can be done with jQuery. The anonymous function is run at page load once all the elements are available in the DOM.

您需要等到 DOM 完成加载。这可以通过 jQuery 来完成。一旦 DOM 中的所有元素都可用,匿名函数就会在页面加载时运行。

<script>
    $(function() {
        $('#mylink').trigger('click');
    });
</script>

回答by Spokey

document.getElementById('mylink').click();


trigger('click')will fire the click event but not the default one.

trigger('click')将触发点击事件,但不会触发默认事件。

$('a').click(function(){ alert('triggered') }) // this will be fired by trigger