How jquery detects if an element exists

Source: Internet
Author: User

How jquery detects whether an element exists:
There may be cases in the code that perform different operations depending on whether the element exists, which requires determining whether the specified element exists.
A collection of matching elements can be obtained using $ (selector), and the length property of the matching element collection can get the number of matching elements in the collection, so as long as the value of the length property is determined to be zero, it is possible to determine whether the element exists. The code example is as follows:

<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metaname= "Author"content= "http://www.51texiao.cn/" /><title>Ant Tribe</title><Scripttype= "Text/javascript"src= "Mytest/jquery/jquery-1.8.3.js"></Script><Scripttype= "Text/javascript">$ (document). Ready (function(){  $("#check"). Click (function(){    if($(". MyTest"). Length>0) {alert ("this element exists"); }  })   })</Script><Body><ul>  <Li>The sun's coming out.</Li>  <Li>Ant Tribe</Li>  <Liclass= "MyTest">Div+css Tutorials</Li></ul><ButtonID= "Check">Click Detect</Button></Body></HTML>

If the number of elements in the collection is greater than 0, then the element exists, otherwise the specified element does not exist.

The original address is: http://www.51texiao.cn/jqueryjiaocheng/2015/0427/243.html

How jquery detects if an element 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.