我有一个通过参数接收对象的函数。 我可以知道这个对象是 div、tr 还是其他对象吗?
我的代码:
function test(obj1, obj2){
alert($.type(obj1) + " --- " + $.type(obj2));
}
function...{
...
test($(this), $(this).parent());
...
}
这样我只能在警报中看到“对象---对象”。有可能知道它是什么物体吗?
最佳答案
element.tagName
非常简单,使用您想要的类型的元素的 tagName
属性。不需要 jQuery。
https://developer.mozilla.org/en-US/docs/Web/API/Element.tagName
在您的代码上下文中:
function test(obj1, obj2){
alert(obj1.get(0).tagName + " --- " + obj2.get(0).tagName);
}
test($(this), $(this).parent());
关于javascript - 如何知道对象的标签类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27127543/