Is there a way to choose: first-word of <p>?

how do we choose p:first-letter ? I know that there is no property called p:first-word , but if any other way SO the user knows.

I do not want to add anything to the HTML.

+4
source share
4 answers

This is not difficult in JavaScript - there is sample code here (you need to look at the source to find out how it works, but it works). As far as I know, this is independent of jQuery or other libraries.

+2
source
 $('p').each(function(){ var me = $(this) , t = me.text().split(' '); me.html( '<strong>'+t.shift()+'</strong> '+t.join(' ') ); }); 

This is a bold first word.

or

 $('p').each(function(){ var me = $(this); me.html( me.text().replace(/(^\w+)/,'<strong>$1</strong>') ); }); 
+6
source

You can do this in JavaScript by finding the DOM node that interests you and grab it .innerHTML by adding around the first word and returning it to .innerHTML.

0
source

You can extract the full text from p and wrap the first word with a regular expression:

 $('p').click(function() { var word = /^\w+/.exec($(this).text()); alert(word); }); 
0
source

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


All Articles