简体   繁体   中英

How to click on a <li> <a/> </li> button in Javascript only?

I want to click on a button like this using Pure javascript:

<ul>
<li class="inactive-link"><a href="/appointment?q=q8345lbf3r9tcmgMnfsad">Schedule Appointment</a></li>
<li class="inactive-link"><a href="/appointment?q=q8345lbf3r9tcmgMnfsad">Schedule Appointment</a></li>
</ul>

Try

 <ul>
<li class="inactive-link"><a href="#">Schedule Appointment</a></li>
<li class="inactive-link"><a href="#">Schedule Appointment</a></li>
</ul>

The question is pretty unclear regarding what you're trying to achieve but for the js part use

 document.querySelector(".inactive-link a").addEventListener('click',()=>{
             window.location="appointment?q=q8345lbf3r9tcmgMnfsad"
    })

Adds the click event to the first li>a element (You can add it to all using forEach). This produces an equivalent result.

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