简体   繁体   English

多段,只有一个“更多阅读”按钮显示全部

[英]Multiple paragraphs, only one read more button to show all

This piece of code loops through all paragraphs and adds a see more button at their end. 这段代码遍历所有段落,并在其末尾添加一个查看更多按钮。 I want it to show just the first few words of the first paragraph and, if you click on "see more", to show all paragraphs. 我希望它仅显示第一段的前几个单词,如果您单击“查看更多”,则显示所有段落。 I tried removing the each loop, tried to wrap it into a div and get it with .html() and then cut it but with no avail. 我尝试删除每个循环,尝试将其包装到div中,并使用.html()进行获取,然后将其剪切但无济于事。 I would appreciate an idea to point me in the right direction. 我希望有一个方向正确的想法。

Update: maybe I wasn't clear enough: just one see more button at the end of first ten characters of the first paragraph. 更新:也许我还不够清楚:在第一段的前十个字符的末尾只有一个看到更多按钮。 If you click on that it shows the full content of all paragraphs. 如果单击它,它将显示所有段落的全部内容。

Please, no plugins. 请没有插件。

 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> 

I have made some changes in your code. 我对您的代码进行了一些更改。 May be this is what you want: 可能这就是您想要的:

 jQuery(function(){ var minimized_elements = $('p'); var counter = 1; minimized_elements.each(function(){ if(counter==1){ $(this).addClass("first"); var t = $(this).text(); if(t.length < 100) return; $(this).html( t.slice(0,100)+'<span>... </span><a href="#" class="more">Show</a>'+ '<span style="display:none;">'+ t.slice(100,t.length)+' <a href="#" class="less">Hide</a></span>' ); } else{ $(this).hide(); } counter++; }); $('a.more', minimized_elements).click(function(event){ event.preventDefault(); $(this).hide().prev().hide(); $(this).next().show(); $('p').show(); }); $('a.less', minimized_elements).click(function(event){ event.preventDefault(); $(this).parent().hide().prev().show().prev().show(); $('p').not('.first').hide(); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/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> 

Your code run, but i tried to figure out your problem so, I edit yours to 您的代码运行了,但是我试图找出您的问题,所以,我将您的代码编辑为

jQuery(function(){

    var minimized_elements = $('p');


    minimized_elements.each(function(){    
        var sefl = this;
        var t = $(this).text();        
        if(t.length < 100) return;

        $(this).html(
            t.slice(0,100)+'<span>... </span><a href="#" class="more">More</a>'+
            '<span class="hidden" style="display:none;">'+ t.slice(100,t.length)+' <a href="#" class="less">Less</a></span>'
        );

        $(this).find('a').click(function() {
            if ( $(this).is('.more') ) {
                $(sefl).find('.hidden').show();
                $(sefl).find('.more').hide();
            } else if ( $(this).is('.less') ) {
                $(sefl).find('.hidden').hide();
                $(sefl).find('.more').show();
            }
        });

    }); 


});

Demo 演示版

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 仅当2个或更多段落时在1段落之后插入按钮 - Insert Button After 1 Paragraph Only If 2 or More Paragraphs 使用相同 ID 的多个按钮在站点上仅切换一个显示更多/显示更少按钮 - Toggle only one show more / show less button on site with multiple buttons of the same id 调试显示更多显示更少按钮 - 缺少文本段落 - Debugging Show More Show Less button - Missing Paragraphs of Text SlideToggle多次单击可读取更多/更少,仅显示一个,隐藏其他。 - SlideToggle multiple click read more/less, show only one, hide others. 如何在具有多个段落的div中的3个段落后添加阅读更多/阅读更少 - how to add read more/read less after 3 paragraphs inside div with multiple paragraphs 显示所有段落 - Show all the paragraphs 如果段落中只有一行,如何隐藏/显示“更多”按钮 - How to hide/show 'more' button if there is only one line in paragraph 仅显示一个段落...无论是否有更多段落,带空白,带空格,带类...仅且仅带CSS - Display only one paragraphs…no matter if there are more, with blank, with white-space, with class…only and only with css jQuery多个切换全部隐藏/仅显示一个 - jQuery multiple toggle hide all / show only one 如果未选中任何复选框,则隐藏按钮;如果单击“全部选中”或单击一个或多个复选框,则显示按钮 - Hide button when no checkbox is checked and show button if Check All is clicked or one or more checkbox is clicked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM