There are two ways to determine the existence of HTML elements in JQuery

  • 2020-03-30 01:03:54
  • OfStack

Encountered such a situation at work, part of the HTML elements on a page is through the server language control display different content according to different situations, however, in some cases on the same page after the DOM is ready to use Ajax to server communication, and some things don't need, so I just want to exist in the page I specified HTML tag I didn't trigger Ajax part, after all, as this can help to reduce the burden of the server, useless talk a lot of, get into the business:

In fact, to solve the problem of determining whether an element exists in JQuery, the method is very simple:

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

If ($(" # ABC "). The HTML ()! = "") {... }

Either way, you can tell if the HTML document has the elements you want. You've already tried using $("# ABC ")! = null /! $(" # ABC ") / $. Find (# "ABC")... The result is not good, because whether the element exists or not will return Object, so I temporarily use one of the above two methods to solve the problem, specific there is a better way, to be discovered......


Related articles: