Two solutions for JQuery to determine whether HTML elements exist

Source: Internet
Author: User

In this case, some HTML elements on the page are controlled by the server language to display different content according to different situations, however, in some cases on the same page, Ajax is required to communicate with the server after DOM preparation, but in some cases, so I only want to trigger the Ajax part when the page contains the HTML Tag I specified. After all, this can reduce the burden on the server. There is a lot of nonsense:

In fact, the method for JQuery to determine whether an element exists is simple:

If ($ ("# abc"). length> 0 ){...}

If ($ ("# abc" ).html ()! = ""){...}

Both of the above methods can determine whether html documents have the elements we want. We have tried $ ("# abc") before ")! = Null /! $ ("# Abc")/$. find ("# abc ")..... the result is not correct. no matter whether the element exists or not, the Object will be returned. Now, I have used one of the above two methods to solve the problem. Is there a better solution ......

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.