使用Object.prototype.toString.call()返回对象类型与Javascript - 不工作在IE中 [英] Using Object.prototype.toString.call() to return object type with Javascript - not working in IE

查看:91
本文介绍了使用Object.prototype.toString.call()返回对象类型与Javascript - 不工作在IE中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望我能以一种可以理解的方式提出这个问题......总而言之,我试图确定我目前正在处理的是哪种类型的对象。

>

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

 < div id =tabContentWrap> 
< div id =tab>
< a href =http:// somelink>链接元素< / a>< img src =img.jpgalt =img/>
< select id =my_select>< option value =1> 1< / option>< / select>
< / div>
< / div>
$ b $ 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 ++){
//这里我试图过滤集合
if(tabContentLinks [k] =='[object HTMLSelectElement ]'){
alert(找到选择列表);




其中在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];
//特殊情况
返回Object.prototype.toString.call(thing);

$ / code>

但是这只是返回 [object Object] code
$ b $ Object.prototype.toString.call()永远返回对象的类型IE或者我离得很远,吠叫一个灯柱而不是一棵树?

解决方案

嗯,首先我想告诉你 Object.prototype.toString 返回对象内部的 [[Class]] 属性的值,它不是't真的是一个类型



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

Javascript只有6种语言类型: Object,String,Number,Boolean,Null和Undefined ,它。



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

你正在使用DOM元素,你想知道它是什么类型的节点,我的建议是简单地使用 nodeName 属性(请避免使用 tagName
$ b

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 ++){
//这里我试图过滤集合
if(tabContentLinks [k] .nodeName =='SELECT '){//< - 选择元素
alert(找到选择列表);
}
}
}
}


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?

解决方案

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");
      }
    }
  }
}

这篇关于使用Object.prototype.toString.call()返回对象类型与Javascript - 不工作在IE中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆