简体   繁体   中英

Get img src from a cell Javascript

I want to take the src of an image of out every cell from a table's tr .

Here's what I am trying to do:

    function returnPics(){
    var x = document.getElementById("table2").rows[0].cells.length;
    for (var i=0 ; i <x ; i++){
        console.log("in");
        var link = document.getElementById("table2").rows[0].cells[i].childNodes[i].src ;
        console.log(link);
    }
}

I have also tried the getAttribute method:

var link = document.getElementById("table2").rows[0].cells[i].getAttribute("src")

If you use jQuery it is really simple.

$('#table2 img').each(function() {
  var link=$(this).attr('src');
  console.log(link);
}

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