Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Выбрать элемент не имеющий детей но имеющий текст (https://javascript.ru/forum/jquery/30164-vybrat-ehlement-ne-imeyushhijj-detejj-no-imeyushhijj-tekst.html)

VitAl2013 26.07.2012 14:05

Выбрать элемент не имеющий детей но имеющий текст
 
Как?

Deff 26.07.2012 14:41

$("body *").each( function() {
 if($(this).children("*").length){return true;}
 else{
   if($(this).text().length){
       alert($(this).html())
   }}
});

walik 26.07.2012 18:33

Можно так еще вроде:
$('body *:not(:has(*)):not(:empty)')


<!DOCTYPE HTML>
<html>
  <head>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.js"></script>
    <style>
      div {height: 20px;}
      div.with_text {background-color: red;}
    </style>
    <script>
      $(function() {
      	$('body *:not(:has(*)):not(:empty)').addClass('with_text');
      });
    </script>
  </head>
  <body>
    <div></div>
    <div>aaaa</div>
    <div>
      <div></div>
    </div>
    <div>bbbb</div>
    <div></div>
    <div></div>
    <div>cccccc</div>
  </body>
</html>

Deff 26.07.2012 19:23

walik,
Мну хотел c not - но он делает повторный проход по массиву - два not - два прохода... Хотя код красивше

VitAl2013 30.07.2012 09:36

http://jsfiddle.net/VitaliyAT/BN7vs/


Часовой пояс GMT +3, время: 06:35.