简体   繁体   中英

How do I select and click using jQuery

<div id="subNav-Requests" class="suit-sub-nav" style="">
  <ul class="page-container">
    <li>
    <a class="navLink0" href="/MyApp/reporting/list/data.do">
        <span>Report Data</span>
    </a>
    </li>
  </ul>
</div>

I have a code like this. I am trying to simulate a click on 'Report Data' . What I am trying is

var nav = 'div#subNav-' + mainNavId; //results in 'div#subNav-Requests'
$(nav+' a.navLink0').trigger('click'); 

No errors, nothing happens. I also tried this

$('a.navLink0').trigger('click'); 

to the same effect. I have to select the subNav-Requests because there are many more like that, for Eg subNav-Reports , subNav-Allergies etc. with the same structure but with many navLinks inside.

Calling click on an element just executes any event handlers attached to the element. It will not send the browser to the href of an a tag. To do that the best I can think of is:

var href = $('a.navLink0').attr('href');
window.location.href = href;

Does that meet your need?

请尝试$('a.navLink0', nav).click();

As far as i know you cant simulate an actual click on the link, what you are doing is simply triggering a click event, but that isnt enough.

You could set a listener for the event by

$('a.navLink0').click(function() {
    window.location = $(this).attr("href");
});

and then triggering the click event, but why are you doing this? wouldnt a redirect fit your purpose better?

window.location = $('a.navLink0').attr("href");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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