Js/jquery determine if a DOM node exists

Source: Internet
Author: User

JS Native judge whether the DOM node exists in the page

The JavaScript native function does not provide a way to judge whether a DOM node exists, and we usually get DOM nodes that are almost document.getelement. Method, a set of object numbers is returned, and we can access each object of the collection through object[0],object[1]. Since the return is a set of numbers, then there is the length property, and length greater than or equal to 1 means that the DOM node exists in the page

Code:

function () {     if(typeofthis. Length>=1)        {return  true;    }      return false ;};

Use:

Assume that the page has the following node

<div> Here is the div node </div><div> here is the div node </div><span> here is the span node </span>

Determine if the node is on the page:

var is_exist = document.getelementsbytagname (' div '). exist (); alert (is_exist);     // true var is_exist = document.getElementsByTagName (' span '). exist (); alert (is_exist);     // true var is_exist = document.getElementsByTagName (' P '). exist (); alert (is_exist);     // false

Note: If you use the document.getElementById () method to get an object, you cannot use the exist () method, because the method that takes the node object by ID returns an empty object without a node, and does not integrate the prototype exist () function. So it will be an error! So if the object is based on the ID can be directly if (obj) so as to determine whether the DOM node exists in the page

jquery determines if a DOM node exists in the page

You can do this.

To add a prototype:

(function($) {    function() {        if($ (this). length>= 1) {            returntrue;        }         return false ;    };}) (jQuery);

How to use:

If the page has the following DOM node to judge:

<div id= "A" > here is the id=a node </div><div> here is the div node </div><div> here is the div node </div><span > Here is the span node </span>

// false // true // true // false

Both of these methods actually determine whether an object exists based on the length property of the object collection.

Js/jquery determine if a DOM node exists

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.