javascript dom td href
How can I get the href url from in the td with dom
<td class="left"><a href="index.php&del=3" oncli开发者_开发知识库ck="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');
})
精彩评论