jquery - How to trigger onclick event on an anchor tag after page load in javascript - Stack Overflow

I was working with an html document in that i've a navigation menu. i want to trigger a click even

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="">

        <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="/">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="/">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="/">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="/">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="/">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="/">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="/">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>

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/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./">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./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./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./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./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./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./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>
Share Improve this question edited May 27, 2014 at 10:15 Niko 1,3771 gold badge13 silver badges37 bronze badges asked Apr 4, 2014 at 15:51 Hybrid DeveloperHybrid Developer 2,3401 gold badge34 silver badges55 bronze badges 5
  • .children.trigger looks very invalid - what does the console say? – tymeJV Commented Apr 4, 2014 at 15:52
  • 1 I see two <head> tags, and no </head> closing tags. – ginman Commented Apr 4, 2014 at 15:55
  • children() not children as it is a function + trigger() will fire handlers you attached, not default browsers behaviours – Stphane Commented Apr 4, 2014 at 16:00
  • And btw, why nesting document ready handler inside window onload handler, looks like you copy/paste code without trying to understand it – A. Wolff Commented Apr 4, 2014 at 16:02
  • sorry i forgot to import jQuery 1.8.3 – Hybrid Developer Commented Apr 4, 2014 at 16:40
Add a ment  | 

2 Answers 2

Reset to default 4

If you want to redirect user simulating anchor click, then call native DOM click method:

$("#menu-item-29").children('a').get(0).click();

Don't forget to include JQuery source when you use $(document).ready

And children() is a function. Look at the documentation : http://api.jquery./children/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745178873a4615324.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信