问题
I was working with an html document in that i've a navigation menu. i want to trigger a click event on the home navigation in onload() iw wrote something but it wont work and i couldn't find a solution on this some one please help me to fix this
code
<html>
<head>
<nav id="navigation" class="navigation" role="navigation" itemscope="" itemtype="http://schema.org/SiteNavigationElement">
<ul id="navigation-primary" class="navigation-primary"><li id="menu-item-29" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-6 current_page_item menu-item-29"><a href="http://example.com/">Home</a></li>
<li id="menu-item-28" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-28"><a href="http://example.com/events/">Gigs</a></li>
<li id="menu-item-31" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-31"><a href="http://example.com/videos/">Videos</a></li>
<li id="menu-item-79" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-79"><a href="http://example.com/gallery/">Gallery</a></li>
<li id="menu-item-149" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-149"><a href="http://example.com/about-us/">Bio</a></li>
<li id="menu-item-477" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-477"><a href="http://example.com/media-kit/">Media Kit</a></li>
<li id="menu-item-514" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-514"><a href="http://example.com/contact-us/">Contact Us</a></li>
</ul>
</nav>
<head>
<body>
fsfsfsfdfs
sfdgdfg
sgvsdfg
dfbd
b
xcfb
dcf
bcb
vb
cvb
cvbc
bvc
bc
bnc
bcb
cb
cb
c
</body>
<script type="text/javascript">
window.onload = function(){
$(document).ready(function(){
$("#menu-item-29").children.trigger('click');
});
};
</script>
</html>
回答1:
If you want to redirect user simulating anchor click, then call native DOM click method:
$("#menu-item-29").children('a').get(0).click();
回答2:
Don't forget to include JQuery source when you use $(document).ready
And children()
is a function. Look at the documentation : http://api.jquery.com/children/
来源:https://stackoverflow.com/questions/22867433/how-to-trigger-onclick-event-on-an-anchor-tag-after-page-load-in-javascript