How can I determine the type of an HTML element in JavaScript?

后端 未结 4 1327
时光说笑
时光说笑 2020-11-30 20:21

I need a way to determine the type of an HTML element in JavaScript. It has the ID, but the element itself could be a

, a
f
4条回答
  •  执念已碎
    2020-11-30 20:28

    You can use generic code inspection via instanceof:

    var e = document.getElementById('#my-element');
    if (e instanceof HTMLInputElement) {}         // 
    elseif (e instanceof HTMLSelectElement) {}    //