我试图调用一个函数,只有当一个HTML元素是空的,使用jQuery。
就像这样:
if (isEmpty($('#element'))) {
// do something
}
我试图调用一个函数,只有当一个HTML元素是空的,使用jQuery。
就像这样:
if (isEmpty($('#element'))) {
// do something
}
当前回答
jQuery.fn.doSomething = function() {
//return something with 'this'
};
$('selector:empty').doSomething();
其他回答
在resume中,有很多选项可以判断一个元素是否为空:
1-使用html:
if (!$.trim($('p#element').html())) {
// paragraph with id="element" is empty, your code goes here
}
2-使用文本:
if (!$.trim($('p#element').text())) {
// paragraph with id="element" is empty, your code goes here
}
3-使用is(':empty'):
if ($('p#element').is(':empty')) {
// paragraph with id="element" is empty, your code goes here
}
4-使用长度
if (!$('p#element').length){
// paragraph with id="element" is empty, your code goes here
}
在addiction中,如果你想知道输入元素是否为空,可以使用val:
if (!$.trim($('input#element').val())) {
// input with id="element" is empty, your code goes here
}
如果你说的“空”是指没有HTML内容,
if($('#element').html() == "") {
//call function
}
试试这个:
if (!$('#el').html()) {
...
}
if($("#element").html() === "")
{
}
空是指不包含文本吗?
if (!$('#element').text().length) {
...
}