您现在的位置是:首页 > 电脑技术查询 > web开发

怎么获得上级标签

编辑:chaxungu时间:2022-10-02 23:21:28分类:web开发

如何获得上级标签?
例如:
<table>
<tr>
<td>
<div>
<input type= "checkbox " name= "aaa " value= "on " checked= "checked ">
</div>
</td>
</tr>
</table>
已经获得了复选框的对象
如何获得tr的对象?

------解决方案--------------------
<table>
<tr>
<td id= "tr1 ">
<div>
<input type= "checkbox " name= "aaa " value= "on " checked= "checked " onclick= "alert(this.parentNode.parentNode.innerHTML);alert(this.parentNode.parentNode.id) ">
</div>
</td>
</tr>
</table>
------解决方案--------------------
//方法
function getParentByTagName(obj,tag)
{
if(obj!=null && obj.tagName!=null && obj.tagName.toLowerCase()!=tag.toLowerCase())obj = obj.parentElement;
return obj;
}

//使用
<input type= "checkbox " name= "aaa " value= "on " checked= "checked " onclick= "alert(getParentByTagName(this, "tr ").innerHTML); ">

------解决方案--------------------
支持mingxuan3000(铭轩) 的做法