객체가 IE의 노드리스트의 인스턴스인지 확인하는 방법은 무엇입니까?
-
02-07-2019 - |
문제
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");
}
아담 프랑코의 대답 거의 공장. 안타깝게도, typeof el.item
IE (7 : String, 8 : Object, 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");
}
나는 항상 특정 유형으로 평가하는 것을 사용합니다. 그런 다음 유효한 개체가 있는지 확인하기 위해 True/False 유형 확인을 수행합니다. 귀하의 경우, 나는 지금과 같은 선택 항목에 대한 참조를 얻은 다음 getOptions () 메소드를 사용하여 옵션을 나타내는 htmlcollection을 얻습니다. 이 객체 유형은 Nodelist와 매우 유사하므로 작업에 문제가 없습니다.
와 함께 jQuery:
if (1 < $(el).length) {
alert("I'm a NodeList");
}
제휴하지 않습니다 StackOverflow