How to find if div with specific id exists in jQuery?

后端 未结 10 1318
我在风中等你
我在风中等你 2020-11-28 17:51

I’ve got a function that appends a

to an element on click. The function gets the text of the clicked element and assigns it to a variable called
10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 18:19

    Try to check the length of the selector, if it returns you something then the element must exists else not.

    if( $('#selector').length )         // use this if you are using id to check
    {
         // it exists
    }
    
    
    if( $('.selector').length )         // use this if you are using class to check
    {
         // it exists
    }
    

    Use the first if condition for id and the 2nd one for class.

提交回复
热议问题