我试图调用一个函数,只有当一个HTML元素是空的,使用jQuery。

就像这样:

if (isEmpty($('#element'))) {
    // do something
}

当前回答

香草javascript解决方案:

if(document.querySelector('#element:empty')) {
  //element is empty
}

请记住,空格会影响空,但注释不会。有关更多信息,请检查MDN关于空伪类。

其他回答

if($("#element").html() === "")
{

}
jQuery.fn.doSomething = function() {
   //return something with 'this'
};

$('selector:empty').doSomething();

你在找jQuery.isEmptyObject()吗?

http://api.jquery.com/jquery.isemptyobject/

你可以试试:

if($('selector').html().toString().replace(/ /g,'') == "") {
//code here
}

*替换空格,以防万一;)

如果你说的“空”是指没有HTML内容,

if($('#element').html() == "") {
  //call function
}