English 中文(简体)
• 如何在 Java山发现超声波/NodeList?
原标题:How to detect HTMLCollection/NodeList in JavaScript?
  • 时间:2011-08-30 02:48:52
  •  标签:
  • javascript

我不敢肯定我目前的执行情况:

function isNodeList(nodes) {
    var result = Object.prototype.toString.call(nodes);
    // modern browser such as IE9 / firefox / chrome etc.
    if (result ===  [object HTMLCollection]  || result ===  [object NodeList] ) {
        return true;
    }
    //ie 6/7/8
    if (typeof(nodes) !=  object ) {
        return false;
    }
    // detect length and item 
    if (!( length  in nodes) || !( item  in nodes)) {
        return false;
    }
    // use the trick NodeList(index),all browsers support
    try {
        if (nodes(0) === null || (nodes(0) && nodes(0).tagName)) return true;
    }
    catch (e) {
        return false;
    }
    return false;
}

A common situation is {length:1,item:function(){return [];}}
The value of result in chrome / safari / opera is [object NodeList] .
In firefox and IE 9 , it is [object HTMLCollection] .

标准价值是什么?

问题回答

如下:

NodeList.prototype.isPrototypeOf(nodes)

@DavidSpector, for HTMLCollection you can similarly use :

HTMLCollection.prototype.isPrototypeOf(collection)

I would structure the code differently:

function isNodeList(nodes) {
    var stringRepr = Object.prototype.toString.call(nodes);

    return typeof nodes ===  object  &&
        /^[object (HTMLCollection|NodeList|Object)]$/.test(stringRepr) &&
        (typeof nodes.length ===  number ) &&
        (nodes.length === 0 || (typeof nodes[0] === "object" && nodes[0].nodeType > 0));
}

注:

  • less return paths make easier-to-read code
  • stick with one type of logic, if possible (i.e. use less negated checks)
  • "item" is not mandatorily in a nodeList
  • use hasOwnProperty() instead of in
  • use square brackets to index into the list
  • I don t think a try/catch is really necessary, but that might be wrong - you decide
  • check for nodeType instead of tagName, as text nodes or comments do not have a name
  • add more checks to the && chain if you see fit

在这里,如果物体在现代浏览器中是无能为力的,如何检验:

if (nodes instanceof NodeList) {
  // It s a NodeList object
}

<>>>>:

Element.prototype.isNodeList = function() {return false;}
NodeList.prototype.isNodeList = HTMLCollection.prototype.isNodeList = function(){return true;}

use like this:

var d; // HTMLCollection|NodeList|Element
if(d.isNodeList()){
  /*
    it is HTMLCollection or NodeList
    write your code here
  */
}else{
  /*
    it is not HTMLCollection and NodeList
    write your code here
  */
}

如果变数是超额收集,或是单质元素

  var foo = document.getElementById( mydiv );
  var foo2 = document.getElementsByClassName( divCollection );
  console.log(foo instanceof HTMLElement);
  console.log(foo instanceof HTMLCollection);

答案可能真的晚了,但......。

if (nodes ==  [object NodeList] ) {
  // It s a nodeList
}

我制定了所有答案的基准,以便看到,最快的是批准。 排在<代码>NodeList.prototype.isPrototypeOf(nodes)上,迄今为止是最快的。 但在正常使用情况下,<代码>诺德列斯/代码>也应予罚款。

我个人不会选择<代码>s NodeList的功能,因为其缓慢、习俗和太多的间接费用。

下面是 Java本功能,以检查在OMM有一个网页上是否存在超声波或无声波,以下代码在上测试。 Chrome Dev Technologies > Console tab of https://stack Exchange.com/:

function ifElemExists(elem) {
    if((elem instanceof NodeList) || (elem instanceof HTMLCollection)) return true;

    return false;
}

const elem1 = document.querySelector( .blah );
const elem2 = document.querySelectorAll( .siteHeader );
const elem3 = document.getElementById( footer );
const elem4 = document.getElementsByClassName( blah );

console.log(ifElemExists(elem1)); // Output: false
console.log(ifElemExists(elem2)); // Output: true
console.log(ifElemExists(elem3)); // Output: false
console.log(ifElemExists(elem4)); // Output: true




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签