Method: 1. Use html() to get the element content, and then use if to determine whether it is empty. The syntax is "if($(selector).html()){//not empty}". 2. Use the ":empty" selector to judge, the syntax is "if($(selector).is(":empty"))".
The operating environment of this tutorial: windows7 system, jquery1.7.2 version, Dell G3 computer.
Recommended tutorial: jQuery tutorial
jquery method to determine whether the element content is empty
1. Use html() Method to obtain the element content, and then use if to judge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> </head> <body> <p>默认文本。测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!</p> <script type="text/javascript"> if($('p').html()) { alert('元素内容不为空') }else{ alert('元素内容为空') } </script> </body> </html>
2. Use the :empty pseudo-class to judge
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script type="text/javascript" src="jquery-1.7.2.min.js"></script> </head> <body> <p>默认文本。测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!测试文本!</p> <script type="text/javascript"> if($('p').is(":empty")) { alert('元素内容为空') }else{ alert('元素内容不为空') } </script> </body> </html>
Rendering:
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of How to determine whether the element content is empty in jquery. For more information, please follow other related articles on the PHP Chinese website!