简体   繁体   English

页面加载上的链接触发器不起作用

[英]link trigger on page load is not working

I'm making a simple example on triggering a click on a link on page load but it's not working. 我正在做一个关于在页面加载时触发对链接的单击的简单示例,但是它不起作用。 I don't want to navigate the that href I just want the trigger to work for something else I'll work on here's the head code 我不想浏览那个href,我只想让触发器对我将要处理的其他东西起作用,这是头代码

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">
$(document).ready(function() {
setTimeout(function() {
    $("a#test").trigger('click');
},1000);
});

</script>

and the body code 和身体代码

<a href="http://www.yahoo.com" id="test">click</a>

Can you try this, 你可以试试这个吗

    $(document).ready(function() { 

        $( "#test" ).on( "click", function() {
             alert('Clicked');
             window.location.href = $( this ).attr('href'); //automatic redirection from the link href
             console.log('clicked');
            });

        setTimeout(function() {
            $("#test").trigger('click');
        },1000);
    });

It's actually working but it doesn't redirect you. 它实际上在工作,但不会重定向您。

You can see an example here. 您可以在此处查看示例。

setTimeout(function() {
    $("a#test").trigger('click');
},1000);
$("a").on('click', function(){
    $(this).css('font-size', 50)
})

Demo 演示版

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

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