使用 Object.prototype.toString.call() 通过 Javascript 返回对象类型 - 在 IE 中不起作用

发布于 2024-10-03 11:03:26 字数 1680 浏览 1 评论 0原文

希望我能以一种可以理解的方式问这个问题......

总的来说,我正在尝试确定我当前正在处理的对象类型。

我正在创建一个集合(HTML 是示例,而不是字面意思),我需要将集合过滤到某些元素,例如:

        <div id="tabContentWrap">
            <div id="tab">
                <a href="http://somelink">Link Element</a><img src="img.jpg" alt="img" />
                <select id="my_select"><option value="1">1</option></select>
            </div>
        </div>

function getFilteredElements() {
    var tabContent = getElementsByClass("tabContentWrap", document.getElementById(tabWrapId), "div");

    for (var j = 0; j < tabContent.length; j++){
        tabContentLinks = tabContent[j].getElementsByTagName('*');
        for (var k = 0; k < tabContentLinks.length; k++){
            // Here i attempt to filter the collection
            if (tabContentLinks[k] == '[object HTMLSelectElement]') {
                alert("found select list");
            }
         }
     }
 }

在 Mozilla 中工作正常,但在 Internet Explorer 8 中不行,tabContentLinks[k] 返回[object] 而不是 [object 'ObjectType']

所以我调查并发现你可以使用 Object.prototype.toString.call(object) 获取对象类型,它在 Mozilla 中再次工作正常,但在 IE8 中返回 [object Object] ...

我调用

get_type(tabContentsLink[k]);

它运行以下函数:

function get_type(thing){
    if (thing === null) return "[object Null]";
    // special case
    return Object.prototype.toString.call(thing);
}

但这仅返回 [object Object]

Object.prototype.toString.call() 是否会在 IE 中返回对象的类型,还是我离得很远,只是在向灯柱而不是树吠叫?

Hopefully I can ask this in an understandable way...

Overall, I am trying to determine what type of object I am currently dealing with.

I'm creating a collection (HTML is example, not literal) and I need to filter my collection to certain elements eg:

        <div id="tabContentWrap">
            <div id="tab">
                <a href="http://somelink">Link Element</a><img src="img.jpg" alt="img" />
                <select id="my_select"><option value="1">1</option></select>
            </div>
        </div>

function getFilteredElements() {
    var tabContent = getElementsByClass("tabContentWrap", document.getElementById(tabWrapId), "div");

    for (var j = 0; j < tabContent.length; j++){
        tabContentLinks = tabContent[j].getElementsByTagName('*');
        for (var k = 0; k < tabContentLinks.length; k++){
            // Here i attempt to filter the collection
            if (tabContentLinks[k] == '[object HTMLSelectElement]') {
                alert("found select list");
            }
         }
     }
 }

Which works fine in Mozilla but not in Internet Explorer 8, tabContentLinks[k] returns [object] instead of [object 'ObjectType']

So I investigated and discovered that you can use Object.prototype.toString.call(object) to get the object type, which again works fine in Mozilla but returns [object Object] in IE8...

I call

get_type(tabContentsLink[k]);

which runs the following function:

function get_type(thing){
    if (thing === null) return "[object Null]";
    // special case
    return Object.prototype.toString.call(thing);
}

But this just returns [object Object]

Does Object.prototype.toString.call() ever return the type of object in IE or am I very far off and barking up a lamppost instead of a tree?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青丝拂面 2024-10-10 11:03:26

好吧,首先我想告诉你,Object.prototype.toString返回的是对象内部[[Class]]属性的值,它并不是真正的键入

此内部属性的值表示对象的规范定义的分类(更多信息此处)。

Javascript 只有 6 种语言类型对象、字符串、数字、布尔、空和未定义,仅此而已。

宿主对象(作为 DOM 元素)的[[Class]]内部属性的值可以是任何东西,它完全依赖于实现 ,在内置对象上使用它是安全的 - 除了 IE 中的一些例外,正如 @Alex 在他的答案中链接的文章中指出的那样 - 。

您正在使用 DOM 元素,并且想要弄清楚它是什么类型的节点,我对此的建议是简单地使用 nodeName 属性 (请避免使用 tagName)

nodeName 属性包含您正在处理的节点的名称(大写),因此您可以这样使用它:

function getFilteredElements() {
  var tabContent = getElementsByClass("tabContentWrap", 
  document.getElementById(tabWrapId), "div");

  for (var j = 0; j < tabContent.length; j++){
    tabContentLinks = tabContent[j].getElementsByTagName('*');
    for (var k = 0; k < tabContentLinks.length; k++){
      // Here i attempt to filter the collection
      if (tabContentLinks[k].nodeName == 'SELECT') { // <- SELECT elements
        alert("found select list");
      }
    }
  }
}

Well, first of all I want to tell you that Object.prototype.toString returns the value of the object's internal [[Class]] property, it isn't really a Type.

The value of this internal property represents the specification defined classification of an object (more info here).

Javascript has only 6 language types: Object, String, Number, Boolean, Null and Undefined, that's it.

The value of the [[Class]] internal property for host objects -as DOM elements- can be anything, it is completely implementation-dependent, on built-in objects is safe to use it -except with some exceptions in IE as @Alex pointed out in the article linked in his answer-.

You are working with DOM elements, and you want to figure out what kind of node it is, my suggestion to this, is to simply use the nodeName property (please avoid using tagName).

The nodeName property contains the name of the node you are dealing it, in upper case, therefore you could use it as this:

function getFilteredElements() {
  var tabContent = getElementsByClass("tabContentWrap", 
  document.getElementById(tabWrapId), "div");

  for (var j = 0; j < tabContent.length; j++){
    tabContentLinks = tabContent[j].getElementsByTagName('*');
    for (var k = 0; k < tabContentLinks.length; k++){
      // Here i attempt to filter the collection
      if (tabContentLinks[k].nodeName == 'SELECT') { // <- SELECT elements
        alert("found select list");
      }
    }
  }
}
蔚蓝源自深海 2024-10-10 11:03:26

我不会重新创建整个讨论和可能的解决方案,而是仅向您指出 博客文章 讨论了这个确切的问题。

Rather than recreate the entire discussion and possible solutions, I'll just point you to a blog post that discusses this exact issue.

风筝有风,海豚有海 2024-10-10 11:03:26

我相信 IE 将返回简单的数据类型,但其他一切都只是一个对象。这是浏览器的限制。我不知道 IE9 是否正在改善这种情况,但这可能对你没有帮助。

I believe IE will return simple data types, but everything else is just an object. It's a browser limitation. I don't know if IE9 is improving the situation, but that probably wouldn't help you anyway.

舟遥客 2024-10-10 11:03:26

我没有方便的 IE,但在 Firefox 中正确的方法是使用 object.constructor.nameobject.constructor 是对象的构造函数,.name 是函数名称)。

function Xyz() {}
new Xyz().constructor.name === 'Xyz'

但随后,还有,

var Abc = function() {}
new Abc().constructor.name === 'Abc'

I don't have IE handy, but the proper way in Firefox is to use object.constructor.name (object.constructor being the object's constructor, .name being the function name).

function Xyz() {}
new Xyz().constructor.name === 'Xyz'

But then, also,

var Abc = function() {}
new Abc().constructor.name === 'Abc'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文