ฉันพยายามเรียกใช้ฟังก์ชันเฉพาะถ้าองค์ประกอบ HTML ว่างเปล่าโดยใช้ jQuery
บางสิ่งเช่นนี้
if (isEmpty($('#element'))) {
// do something
}
ฉันพยายามเรียกใช้ฟังก์ชันเฉพาะถ้าองค์ประกอบ 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())=='')
if(!$.trim($("selector").html())
if($("selector").children().length === 0)
if($("selector > *").length === 0)
พื้นที่สีขาวและตัวแบ่งบรรทัดเป็นปัญหาหลักของการใช้: ตัวเลือกที่ว่างเปล่า ระวังใน CSS คลาส pseudo ที่ว่างเปล่าทำงานในลักษณะเดียวกัน ฉันชอบวิธีนี้:
if ($someElement.children().length == 0){
someAction();
}
$.children()
ไม่กลับข้อความหรือแสดงความคิดเห็นโหนดซึ่งหมายถึงการตรวจสอบการแก้ปัญหานี้เพียงอย่างเดียวคือองค์ประกอบที่ว่างเปล่าขององค์ประกอบ หากองค์ประกอบที่มีเพียงข้อความหรือความคิดเห็นไม่ควรพิจารณาว่าว่างเปล่าสำหรับความต้องการของคุณคุณควรใช้วิธีแก้ไขปัญหาอื่น
select
องค์ประกอบที่สมบูรณ์แบบ if(! $el.children().length)
สภาพยังสามารถ
!elt.hasChildNodes()
ใช่ฉันรู้ว่านี่ไม่ใช่ jQuery ดังนั้นคุณสามารถใช้สิ่งนี้:
!$(elt)[0].hasChildNodes()
ตอนนี้มีความสุข?
filter
ฟังก์ชั่นjQuery เพราะฉันไม่ต้องการใช้ jQuery ภายในฟังก์ชั่นเว้นแต่จำเป็น
<div class="results"> </div>
คำสั่งนี้จะคืนค่าเท็จ jsfiddle.net/ytliuSVN/0pdwLt46
jQuery.fn.doSomething = function() {
//return something with 'this'
};
$('selector:empty').doSomething();
this
นอกเหนือจากองค์ประกอบ jQuery @ZealMurapa
ถ้าโดย "empty" คุณหมายถึงไม่มีเนื้อหา 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
}
* แทนที่ช่องว่างสีขาวเพียงใส่หีบ)
!/[\S]/.test($('Selector').html())
ทำงานได้ดีขึ้นเมื่อคุณพบช่องว่างที่คุณรู้ว่ามันว่างเปล่า
The production CharacterClassEscape :: S evaluates by returning the set of all characters not included in the set returned by CharacterClassEscape :: s.
มาจากecma-international.org/ecma-262/5.1/#sec-15.10.2.12
if($("#element").html() === "")
{
}
คุณกำลังมองหา jQuery.isEmptyObject()
อะไร?
นี่คือตัวกรอง jQuery ตามhttps://stackoverflow.com/a/6813294/698289
$.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()) {
...
}
การขึ้นบรรทัดใหม่ถือเป็นเนื้อหาขององค์ประกอบใน FF
<div>
</div>
<div></div>
Ex:
$("div:empty").text("Empty").css('background', '#ff0000');
ใน IE ทั้ง divs นั้นจะถือว่าว่างเปล่าใน FF an 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
}
})
$('#elem').text().match(/\S/) || alert('empty');