简体   繁体   中英

How to click on anchor tag inside table <td> tag

I am trying to click anchor tag inside table data. I can traverse inside table till this much:

This is part of my HTML:

<td class="table-active" scope="row">
    <a href="https://www.google.com"> Confirm </a>
       <br>
    <a href="https://www.gmail.com"> Reject </a>
</td>

And this is what tried:

var x = document.getElementsByTagName("TABLE");

x[0].rows[1].cells[7]

Click on either confirm link / reject link enter code here

Not sure if you are using ES6 or any modern features of JS.

But to get you started and to create a click event for each anchor tag do the following:

var table = document.querySelector("table");

for(var i = 0 ; i < table.rows.length; i++) {
    var row = "";

    for (var j = 0; j < table.rows[i].cells.length; j++) {
        console.log(table.rows[i].cells[j]);
        table.rows[i].cells[j].addEventListener('click', function(e) {
            e.preventDefault();
            console.log('clicked');
        })
    }
}

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