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