PHP/jQuery缩短字符串并在点击时显示更多 [英] PHP/jQuery Shorten string and show more on click
问题描述
我有一个动态页面,可从数据库加载用户内容.如果用户的帖子超过50个字符,我将回显此代码:
$data['string'] = substr($data['string'], 49, 50).$showmore
其中$showmore = '<a href="#" class="showmore">Show More</a>
一切工作到此为止,当单击显示更多"链接时,我只是不知道如何使用jQuery更改substr值.如果需要其他信息,我将进行更新.
I have a dynamic page that loads user content from a database. If the user's post is greater than 50 characters, I echo this code:
$data['string'] = substr($data['string'], 49, 50).$showmore
where $showmore = '<a href="#" class="showmore">Show More</a>
Everything works up to this point, I just don't know how to change the substr values with jQuery when the "Show More" link is clicked. I will update this if additional info is needed.
推荐答案
如果从PHP输出所有文本,则可以使用此Fiddle使事情变得有趣.
If you output all the text from PHP, you can use this Fiddle to make things interesting.
提琴: http://jsfiddle.net/iambriansreed/bjdSF/
jQuery(function(){
var minimized_elements = $('p.minimize');
minimized_elements.each(function(){
var t = $(this).text();
if(t.length < 100) return;
$(this).html(
t.slice(0,100)+'<span>... </span><a href="#" class="more">More</a>'+
'<span style="display:none;">'+ t.slice(100,t.length)+' <a href="#" class="less">Less</a></span>'
);
});
$('a.more', minimized_elements).click(function(event){
event.preventDefault();
$(this).hide().prev().hide();
$(this).next().show();
});
$('a.less', minimized_elements).click(function(event){
event.preventDefault();
$(this).parent().hide().prev().show().prev().show();
});
});
这篇关于PHP/jQuery缩短字符串并在点击时显示更多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!