[英]How do I check if an HTML element is empty using jQuery?
我正在嘗試僅在HTML元素為空時使用jQuery調用函數。
像這樣的東西:
if (isEmpty($('#element'))) {
// do something
}
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插件,但你明白了。
我發現這是唯一可靠的方法(因為Chrome和FF將空格和換行符視為元素):
if($.trim($("selector").html())=='')
使用空白選擇器時,空格和換行符是主要問題。 小心,在CSS中:empty偽類的行為方式相同。 我喜歡這種方法:
if ($someElement.children().length == 0){
someAction();
}
!elt.hasChildNodes()
是的,我知道,這不是jQuery,所以你可以使用這個:
!$(elt)[0].hasChildNodes()
現在開心?
jQuery.fn.doSomething = function() {
//return something with 'this'
};
$('selector:empty').doSomething();
如果是“空”,則表示沒有HTML內容,
if($('#element').html() == "") {
//call function
}
空如在包含沒有文字?
if (!$('#element').text().length) {
...
}
在簡歷中,有很多選項可以確定元素是否為空:
1-使用html
:
if (!$.trim($('p#element').html())) {
// paragraph with id="element" is empty, your code goes here
}
2-使用text
:
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-使用length
if (!$('p#element').length){
// paragraph with id="element" is empty, your code goes here
}
如果你試圖找出輸入元素是否為空,你可以使用val
:
if (!$.trim($('input#element').val())) {
// input with id="element" is empty, your code goes here
}
與html()
或children()
相比,瀏覽器需要更少“工作”的另一個選項:
function isEmpty( el ){
return !el.has('*').length;
}
document.getElementById("id").innerHTML == "" || null
要么
$("element").html() == "" || null
你可以試試:
if($('selector').html().toString().replace(/ /g,'') == "") {
//code here
}
*替換白色空間,只需更換;)
你在尋找jQuery.isEmptyObject()
嗎?
這是一個基於https://stackoverflow.com/a/6813294/698289的jQuery過濾器
$.extend($.expr[':'], {
trimmedEmpty: function(el) {
return !$.trim($(el).html());
}
});
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.
試試這個:
if (!$('#el').html()) {
...
}
if($("#element").html() === "")
{
}
換行符被視為FF中元素的內容。
<div>
</div>
<div></div>
例如:
$("div:empty").text("Empty").css('background', '#ff0000');
在IE中,兩個div都被認為是空的,在FF中Chrome只有最后一個是空的。
您可以使用@qwertymk提供的解決方案
if(!/[\S]/.test($('#element').html())) { // for one element
alert('empty');
}
要么
$('.elements').each(function(){ // for many elements
if(!/[\S]/.test($(this).html())) {
// is empty
}
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.