[英]Why does document.getElementsByName return a NodeList while document.getElementsByClassName returns an HTMLCollection?
[英]getElementsByName returns nodelist or htmlcollection
完全不熟悉javascript。 跑了一個getElementsByName查詢並獲得了一個節點列表,但是我正在尋找一個htmlcollection。 我知道這是由於Dom級實施。
問題:我是否應該測試返回類型,以確保結果是否可靠?
function isNodeList(nodes) {
var result = Object.prototype.toString.call(nodes);
if (typeof nodes === 'object' && /^\[object HTMLCollection|NodeList|Object)\]$/.test(result) && nodes.hasOwnProperty('length') && (nodes.length == 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0))) {
return true;
}
return false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.