Проверка вложенных li у ul
Друзья, есть проблема проверки вложенных элементов li у ul. На странице есть куча ul списков с одинаковым id. Проблема в том, что сейчас JS наследует значения и для второго списка показывает тоже сообщение про пустоту. Помогите поправить.
html: <ul id="products1"> </ul> <ul id="products1"> <li><li> </ul> js: <script> $(document).ready(function(){ if( ($("ul#products1").has("li").length == 0) ) { $("ul#products1").html("This <i>unordered list (products1)</i> is <span class='green'>empty</span>"); } }); </script> |
coolplayer37,
циклом по атрибуту id |
coolplayer37,
или так <!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { $("ul[id='products1']").filter(":not(:has(li))").html("This <i>unordered list (products1)</i> is <span class='green'>empty</span>"); }); </script> </head> <body> <ul id="products1"> </ul> <ul id="products1"> <li>test</li> </ul> <ul id="products1"> </ul> </body> </html> |
Цитата:
<!DOCTYPE html> <html> <head> <title>Untitled</title> <meta charset="utf-8"> <style type="text/css"> </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <script> $(function() { $("ul[id='products1']").each(function(indx, el){ $(el).has("li").length || $(el).html("This <i>unordered list (products1)</i> is <span class='green'>empty</span>"); }); }); </script> </head> <body> <ul id="products1"> </ul> <ul id="products1"> <li>test</li> </ul> <ul id="products1"> </ul> </body> </html> |
Часовой пояс GMT +3, время: 14:58. |