为什么节点列表未定义在IE6/7吗?

<form action="/" method="post" id="testform">
    <input type="checkbox" name="foobar[]" value="1" id="" />
    <input type="checkbox" name="foobar[]" value="2" id="" />
    <input type="checkbox" name="foobar[]" value="3" id="" />    
</form>

<script type="text/javascript" charset="utf-8">
(function () {
    var el = document.getElementById('testform')['foobar[]']
    if (el instanceof NodeList) {
        alert("I'm a NodeList");
    }  
})();
</script>

这一工作在FF3/Safari3.1但不工作,在IE6/7.任何人有任何想法,如何检查,如果埃是一个实例的节点列表所有浏览器?

没有正确的解决方案

其他提示

Duck Typing &quot;应该始终有效:

...

if (typeof el.length == 'number' 
    && typeof el.item == 'function'
    && typeof el.nextNode == 'function'
    && typeof el.reset == 'function')
{
    alert("I'm a NodeList");
}

亚当佛朗哥的答案 几乎 工程。不幸的是, typeof el.item 返回不同的东西的不同版本的即(7:string,8:对象9:功能)。因此,我利用他的代码,但是我改变的行为 typeof el.item !== "undefined" 并改变了 ===== 在整个。

if (typeof el.length === 'number' 
    && typeof el.item !== 'undefined'
    && typeof el.nextNode === 'function'
    && typeof el.reset === 'function')
{
    alert("I'm a NodeList");
}

我会使用总是评估某种类型的东西。然后你只需要进行真/假类型检查,看看你是否有一个有效的对象。在你的情况下,我会像你现在一样获得对select项的引用,然后使用它的getOptions()方法来获取表示选项的HTMLCollection。此对象类型与NodeList非常相似,因此使用它时应该没有问题。

使用 jQuery

if (1 < $(el).length) {
    alert("I'm a NodeList");
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top