我试图调用一个函数,只有当一个HTML元素是空的,使用jQuery。
就像这样:
if (isEmpty($('#element'))) {
// do something
}
我试图调用一个函数,只有当一个HTML元素是空的,使用jQuery。
就像这样:
if (isEmpty($('#element'))) {
// do something
}
你可以试试:
if($('selector').html().toString().replace(/ /g,'') == "") {
//code here
}
*替换空格,以防万一;)
jQuery.fn.doSomething = function() {
//return something with 'this'
};
$('selector:empty').doSomething();
if ($('#element').is(':empty')){
//do something
}
更多信息请参见http://api.jquery.com/is/和http://api.jquery.com/empty-selector/
编辑:
正如一些人指出的那样,浏览器对空元素的解释可能会有所不同。如果你想忽略不可见的元素,如空格和换行符,并使实现更加一致,你可以创建一个函数(或只使用它里面的代码)。
function isEmpty( el ){
return !$.trim(el.html())
}
if (isEmpty($('#element'))) {
// do something
}
你也可以把它变成一个jQuery插件,但你知道的。
换行符被认为是FF中元素的内容。
<div>
</div>
<div></div>
Ex:
$("div:empty").text("Empty").css('background', '#ff0000');
在IE中,两个div都被认为是空的,在FF和Chrome中,只有最后一个是空的。
您可以使用@qwertymk提供的解决方案
if(!/[\S]/.test($('#element').html())) { // for one element
alert('empty');
}
or
$('.elements').each(function(){ // for many elements
if(!/[\S]/.test($(this).html())) {
// is empty
}
})
!elt.hasChildNodes()
是的,我知道,这不是jQuery,所以你可以使用这个:
!$(elt)[0].hasChildNodes()
现在快乐吗?
空格和换行符是使用:empty选择器的主要问题。注意,在CSS中:empty伪类的行为与此相同。我喜欢这个方法:
if ($someElement.children().length == 0){
someAction();
}
document.getElementById("id").innerHTML == "" || null
or
$("element").html() == "" || null
下面是一个基于https://stackoverflow.com/a/6813294/698289的jQuery过滤器
$.extend($.expr[':'], {
trimmedEmpty: function(el) {
return !$.trim($(el).html());
}
});
对于浏览器来说,另一个选项应该比html()或children()要求更少的“工作”:
function isEmpty( el ){
return !el.has('*').length;
}
JavaScript
var el= document.querySelector('body');
console.log(el);
console.log('Empty : '+ isEmptyTag(el));
console.log('Having Children : '+ hasChildren(el));
function isEmptyTag(tag) {
return (tag.innerHTML.trim() === '') ? true : false ;
}
function hasChildren(tag) {
//return (tag.childElementCount !== 0) ? true : false ; // Not For IE
//return (tag.childNodes.length !== 0) ? true : false ; // Including Comments
return (tag.children.length !== 0) ? true : false ; // Only Elements
}
尝试使用其中任何一种!
document.getElementsByTagName('div')[0];
document.getElementsByClassName('topbar')[0];
document.querySelectorAll('div')[0];
document.querySelector('div'); // gets the first element.
在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
}
香草javascript解决方案:
if(document.querySelector('#element:empty')) {
//element is empty
}
请记住,空格会影响空,但注释不会。有关更多信息,请检查MDN关于空伪类。