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

javascript如何判断当前元素是tr还td

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

javascript怎么判断当前元素是tr还td。
var myElement=document.getElementById("idname");

已知得到的myElement元素为tr或td,那怎么判断得到的是tr还是td呢?

------解决方案--------------------
myElement.tagName ?
------解决方案--------------------
探讨
myElement.tagName ?

------解决方案--------------------
var myElement=document.getElementById("idname");
if(myElement.tagName === 'TR'){
alert('This is a tr!');
}else if(myElement.tagName === 'TD'){
alert('This is a td!');
}