2011-09-24 3 views
0

Je cherchais un script pour mettre un saut de ligne après un certain nombre de mots. J'ai trouvé ici en stackoverflow: Applying a style to and inserting a line break after the first word in a linkAjouter un saut de ligne après quelques mots (ex 8)

Alors, je change un peu:

var el = $('.wtt'); //where wtt is the class of the elements that you want to add the <br /> 
var text = obj.html(); 
var parts = text.split(' '); 
//number 7 represents 8 words 
$('.wtt').html($('.wtt').html().replace(parts[7],parts[7]+'<br />')); 
+1

Toute objection contre CSS: '.wtt {width: 200px; word-wrap: mot de passe; white-space: normal;} '? –

Répondre

0

Ce qui suit remplacerait l'espace 8 dans une chaîne avec un <br>

var text = 'hello there purple giraffe, how are you doing today.'; 
alert(text.replace(/^((\S+\s+){7}\S+)\s+/, '$1<br>')); 

\S+ correspond à un ou plus de caractère non-espace. correspond à un ou plusieurs caractères spatiaux.

Questions connexes