How to check that a div exists jquery and if it does do nothing

This question already has an answer here:

  • Is there an “exists” function for jQuery? 36 answers

  • 检查一个jQuery对象的长度属性

    if($('div.errormsg').length==0)
    {
     //Not exists, so redirect
     //window.location.href='newurl';
    }
    

    change this:

    if (document.getElementById('.errormsg')) {
    

    to this:

    if (document.getElementsByClassName('errormsg').length) {
    

    document.getElementsByClassName docs @ MDN


    您可以使用.length属性来检查元素/元素的存在性:

    if(!$('.errormsg ').length){//does ot exist
      //redirect
    }
    
    链接地址: http://www.djcxy.com/p/14688.html

    上一篇: 检查页面上是否插入元素

    下一篇: 如何检查一个div是否存在jquery,以及它是否无效