简体   繁体   中英

javascript dom td href

如何使用dom从td中获取href网址

<td class="left"><a href="index.php&del=3" onclick="return confirmnDelete()"><img src="delete.png" style="border: none"/></a></td>

Not really enough info given, but:

alert(document.getElementById("theTable").rows[0].cells[0].firstChild.href);

http://jsfiddle.net/QaKmx/1

It'd be helpful if you had an ID assigned to that anchor, but if you want to make no changes to markup:

var allCells = document.getElementByTagName("TD");
for(var i = 0; i < allCells.length; i++){
     if(allCells[i].className == "left"){
          if(allCells[i].firstChild && allCells[i].firstChild.tagName.toLowerCase() == "a"){
     if(allCells[i].firstChild.onclick.toString().replace("return confirmDelete()", "") != allCells[i].firstChild.onclick.toString()){
     return allCells[i].firstChild.href;
}
     }
}

Are you able to use a dom manipulation library such as jquery? If so, then something like:

$('.left > a').click(function(e) {
    e.preventDefault();
    var url = $(this).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