3
I managed to do the function of See More, with a code here from Stackoverflow, but the texts come from the dynamic database, or whatever I do to get each text?
Code in php:
< div class="box-body" id="texto">
<?php
$texto = htmlentities($row['texto']);
$texto = preg_replace('/[\n\r]{1,}/',"\n\n",$texto);
echo nl2br(emoticons($texto));
?>
</ div>
Code in Jquery:
var wordLimit = 50;
$(function() {
//trata o conteúdo na inicialização da página
$('#texto').each(function() {
var post = $(this);
var text = post.text();
//encontra palavra limite
var re = /[\s]+/gm, results = null, count = 0;
while ((results = re.exec(text)) !== null && ++count < wordLimit) { }
//resume o texto e coloca o link
if (results !== null && count >= wordLimit) {
var summary = text.substring(0, re.lastIndex - results[0].length);
post.text(summary + '...');
post.data('original-text', text);
post.append('<br/><a href="#" class="read-more">Leia mais</a>');
}
});
//ao clicar num link "Leia mais", mostra o conteúdo original
$('.read-more').on('click', function() {
var post = $(this).closest('#texto');
var text = post.data('original-text');
post.text(text);
});
});
Florida, can you help me?
– Vinicius Henzel
Sorry, I can’t, but if you wait a little while for someone with more experience than I might help you, as it’s the weekend, maybe they’re not around. Remember, the more details you give about what you’re doing and how you want it to look, the easier it will be for someone interested to understand and help you.
– Florida
@Florida, look at hard, it’s the third post I do here and only in vacuum :\
– Vinicius Henzel
We can’t force anyone to answer anything. While someone doesn’t answer, you try to solve it, and if you do, you can answer your own question.
– Florida
A question, what do you mean by dynamic texts? It means that this text may be of varying size or that there may be entries in the table where Voce saves this text and that they should be pulled simultaneously?
– MarceloBoni
That will be several texts coming from the database. I even managed to put a different id for each DIV, but then I would have to put each script for each div, it would look terrible, I wanted only one for all.
– Vinicius Henzel
You cannot have the same id on more than one element. Use a class and swap
#texto
for.sua-classe
in jquery.– bfavaretto
But there will be several Ivs with the same class for example
– Vinicius Henzel
< div class="hi">text 1</ div> < div class="hi">text 2</ div>
– Vinicius Henzel
And so it doesn’t work :\
– Vinicius Henzel
I got it, thank you very much, that’s right, I don’t know why
– Vinicius Henzel
@Viniciushenzel now you can answer your own question if you want, will help others in the future. :)
– Florida
Thank you @Florida
– Vinicius Henzel
Just an addendum about having several elements with the same class, well, that’s the function of the class, grouping similar elements that are the same, you see, class...haha
– Kenny Rafael