jquery click() on a link

冷暖自知 提交于 2019-12-12 02:47:22

问题


I'm trying to trigger a click on a link when a page loads via ajax call, I tried all methods I found, but I can't manage to do that.

Considering that the link on which I want to trigger the click is given by this code

<a href="download.php?file=cloud/index.html" target="_blank" id="cliccami">Click here</a>

Here are the codes that I tried

<script> $("#cliccami").trigger("click"); </script>
<script> $("#cliccami")[0].click(); </script>
<script> window.open($("#cliccami").attr("href"), "_blank"); </script>

Both script and element are in the page loaded via ajax call. Could anyone help me to find out what's the matter?


回答1:


EDIT, I think this is what you're looking for:

html:

<a href="http://www.google.com" target="_blank" id="cliccami">Click here</a>

jQuery:

$(document).ready(function() {

    $('#cliccami').click(function(e) {
        window.open($(this).attr('href'));
        e.preventDefault();
    });

    $('#cliccami').click();
});

In my example (JSFiddle: http://jsfiddle.net/qw9BW/) #cliccami's click event is captured, alerted, then stopped. then a $.click() is ran on page load to force the click event to happen.




回答2:


Have you tried using a ready function?

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


来源:https://stackoverflow.com/questions/23584440/jquery-click-on-a-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!