Hello. There is an element:

<div class="wrapper"> тут много всяких дочерних элементов, в том чисел вложенных друг в друга </div> 

How can I use jquery to get only those descendants (including nested ones) #wrapper that contain text (no matter what, just text) and change the font size for them?

Thank.

  • $ (this) .text () does not work as it should. He chooses and elements with text inside and all their ancestors. - Samat Zhanbekov
  • It's not entirely clear how you imagine changing the font. If you specify any changes for the parent, the children will inherit them. If the text is wrapped with some kind of tag, then it is somehow easier. Is it possible to change the markup, or is it already different?) Perhaps a more accurate example would give more insight. - Bookin
  • Do you want to apply the changes without considering the parent or the children? - Bookin

1 answer 1

I would suggest so

 $(".wrapper *").each(function(){ if ($(this).text().trim().length) { $(this).addClass("right"); }}); 

but the approach has cons. And the first minus is that it will add a class even where there is no text, but there is a child element with text. http://jsfiddle.net/ovzfvrgk/1/ PS I noticed the comments - he essentially answers the same question

Edit:

 $(".wrapper *").each(function(){ var element = $(this); var isParent= element.get(0).childElementCount>0; if (element.text().trim().length && !isParent) { element.addClass("right"); } }); 

If I understand you correctly, then this option should help - it will not mark the parent unit.

http://jsfiddle.net/ovzfvrgk/5/

  • If it was not so easy to ask was :) Of course $ (this) .text () is the problem. He increases the font for me and elements with text inside and all their parents of all levels of nesting. - Samat Zhanbekov