Why jQuery trigger or triggerHandler doesn't work?

孤街醉人 提交于 2020-01-06 04:24:26

问题


Here is my code :

<html>
<head>
     <script src="//code.jquery.com/jquery-1.10.2.js"></script>

     <script type='text/javascript'>
     window.onload=function(){

     var e = jQuery.Event( "click" );
     e.preventDefault();
     $("#rrx").triggerHandler(e);
     $("#rrx").trigger("click");

    }  

    </script>

</head>
<body>

    <a href="http://google.com" id="rrx" > gooooooog </a>

</body>
</html>

After page load I should be redirected to http://google.com but this is not happening!


回答1:


You need to call native click method on anchor tag, jQuery trigger method cannot do it, so you need:

window.onload = function () { $("#rrx")[0].click(); }

Which is the same as:

window.onload = function () { document.getElementById('rrx').click(); }


来源:https://stackoverflow.com/questions/21887238/why-jquery-trigger-or-triggerhandler-doesnt-work

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