问题
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