gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 04:26:01 25 4
gpt4 key购买 nike

为什么 NodeList 在 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/Safari 3.1 中有效,但在 IE6/7 中无效。任何人都知道如何检查 el 是否是跨所有浏览器的 NodeList 实例?

最佳答案

"Duck Typing "应该始终有效:

...

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

关于javascript - 如何检查一个对象是否是 IE 中 NodeList 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/151348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com