Div text selector without p-tags

Is there a selector for the text inside this div without wrapping it in the <p> or maybe with jquery / javascript to find all the text and wrap it in the <p> ?

 <div class="content"> Some content in here. <img src="image" alt="image" /> </div> 

I can’t apply extra HTML markup and want to style the text differently with the image.

I would either wrap the text <p> to become:

 <div class="content"> <p>Some content in here.</p> <img src="image" alt="image" /> </div> 

Or can I somehow style it with CSS, selecting only text without HTML markup?

+4
source share
1 answer

You can select all the bare texts (which are text nodes) using .contents , after which you should filter with .nodeType === 3 (filter text nodes that have nodeType of 3).

For example: http://jsfiddle.net/ZAYD2/ .

 $("div").contents().filter(function() { return this.nodeType === 3; }).wrap("<p>"); 

Then you can use CSS to style all the <p> elements, for example.

+6
source

Source: https://habr.com/ru/post/1383283/


All Articles