这段代码循环浏览所有段落,并在其末尾添加一个查看更多按钮。我希望它只显示第一段的前几个单词,如果单击“查看更多”,则显示所有段落。我尝试删除每个循环,试图将其包装到一个div并获得.html(),然后将其切割但无济于事。我会很高兴有一个想法指向正确的方向。多段落,只有一个阅读更多按钮,以显示全部
更新:也许我不够清楚:只有一个在第一段的前十个字符的末尾看到更多按钮。如果你点击它,它会显示所有段落的全部内容。
请不要插件。
jQuery(function(){
var minimized_elements = $('p');
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();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>
<p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text.</p>
问题是什么??它对我来说不清楚。你不想使用'each()'? – Manwal
为什么downvoted,downvoter? –
你还没有解释是什么问题。你的代码不工作吗? – Andy