jQuery :empty 選擇器
jQuery 中的 :empty 選擇器用來選擇所有的空元素。
語法
語法如下 −
$(":empty")
示例
我們現在看一個使用 :empty() 選擇器的例子 −
<!DOCTYPE html> <html> <head> <style> .demo { color: brown; background-color: orange; font-size: 16px; border: 2px blue dashed; width:100px; height:70px; } </style> <script src="https://code.jquery.com/jquery-3.4.1.js"></script> <script> $(document).ready(function(){ $("p:empty(.one)").addClass("demo"); }); </script> </head> <body> <h2>Enter details</h2> <p class="one"></p> <form action=""> ID: <input type="text" name="id"><br> Rank: <input type="text" name="rank"><br> Fav. Subjects: Maths: <input type="checkbox" name="sub" value="maths"> English <input type="checkbox" name="sub" value="english"><br> <input type="submit" value="Submit"><br> </form> </body> </html>
輸出
這將產生如下輸出 −
廣告