如何在 IE 中检查一个对象是否是 NodeList 的实例?

2023-11-27

为什么在 IE6/7 中 NodeList 未定义?

<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/Safari 3.1,但不适用于 IE6/7。任何人都知道如何检查 el 是否是所有浏览器中的 NodeList 实例?


"鸭子打字“ 应该始终有效:

...

if (typeof el.length == 'number' 
    && typeof el.item == 'function'
    && typeof el.nextNode == 'function'
    && typeof el.reset == 'function')
{
    alert("I'm a NodeList");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 IE 中检查一个对象是否是 NodeList 的实例? 的相关文章

随机推荐