简体   繁体   English

无法使用 jquery 应用截断段落

[英]unable to apply Truncate paragraph using jquery

i have written a small jquery function for Truncateing paragraph.我已经为截断段落编写了一个小的 jquery 函数。 But it does not work for me.但它对我不起作用。 ie it does not show a link for me "show more" or "show less".即它没有为我显示“显示更多”或“显示更少”的链接。

Please help me to solve my problem.请帮我解决我的问题。

My code works perfect if my paragraph is simple/manual.如果我的段落很简单/手动,我的代码就可以完美运行。 But if i show a paragraph added through a rich text editor,which contains some bold words, para etc Then a face problem.(ie it does not show a link for me "show more" or "show less".)但是,如果我显示通过富文本编辑器添加的段落,其中包含一些粗体字、段落等,那么就会出现一个面部问题。(即它没有为我显示“显示更多”或“显示更少”的链接。)

eg.例如。

works good if my para is (without any <b>,<p> )**如果我的 para 是(没有任何<b>,<p> )**

<p class="descpara">  
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem      </p>

Does not work if i include( <b>..</b> ,<p>..</p> )如果我包含( <b>..</b> ,<p>..</p><b>..</b> ,<p>..</p>则不起作用

  <p class="descpara">  
<b>Lorem Ipsum </b>is simply <p> dummy text of the printing and typesetting industry.</p> Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem 
       </p>

My jquery function我的jQuery函数

jQuery(function()
        { 
        var minimized_elements = $('p.descpara');

         var desc_minimized_elements = $('p.descpara');
        minimized_elements.each(function()
        {    
             var t = $(this).text();        
             if(t.length < 300) return;

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

         });  
         $('a.more', minimized_elements).click(function(event){
             event.preventDefault();
             $(this).hide("fast").prev().hide("slow");
             $(this).next().show("fast");        
         });

         $('a.less', minimized_elements).click(function(event){
             event.preventDefault();
             $(this).parent().hide().prev().show().prev().show();    
         }); 
       });

and in my view.php page在我的 view.php 页面中

   <div class="products">
         <div id="bookcont">
           <?php echo"<div id='btitle'>$row->book_title</div></br>";  
               echo "<p>by $row->auth_firstname   $row->auth_lastname</p>"; ?> 
             <div class="detail"> 

             <!--  A long text paragraph, i am apply for this -->
              <p class="descpara">  
                   <?php echo $row->description?>
               </p> 
         </div> 
       </div> 

Current output after apply code as said :应用代码后的当前输出如下:在此处输入图片说明

The problem is that you have <p> inside a <p>问题是你在<p>里面有<p>

Solution: use <div> instead of <p> for the container, see here: http://jsfiddle.net/tbE8V/1/解决方案:使用<div>而不是<p>作为容器,请参见此处: http : //jsfiddle.net/tbE8V/1/

Also note if you want to keep the rich text you need to extract the container html not text另请注意,如果要保留富文本,则需要提取容器html而不是text

var minimized_elements = $('div.descpara');  // <--- note div not p  (change in html as well)

     var desc_minimized_elements = $('div.descpara');
    minimized_elements.each(function()
    {    
         var t = $(this).html();    // <--- note taken html not text

Also - that your solution may have problems with rich text(!!!) ie.另外 - 您的解决方案可能存在富文本问题(!!!),即。 if the 300 character limit may end up in between <b> and </b> , or even in the middle of the element name (eg. in the middle of " <small> ")如果 300 个字符的限制可能在<b></b> ,甚至在元素名称的中间(例如,在“ <small> ”的中间)


edit:编辑:

To handle the rich text problem, I suggest your javascript should check the height of the container element, not the number of characters in the string.为了处理富文本问题,我建议您的 javascript 应该检查容器元素的高度,而不是字符串中的字符数。 If it is more than the desired height then apply a "hide-overflow" css class which will have max-height and overflow:hidden, and also add the "more" link.如果它超过所需的高度,则应用“隐藏溢出”css 类,该类将具有最大高度和溢出:隐藏,并添加“更多”链接。 Clicking the "more" will just remove the "hide-content" class from the container and change the link to "less".单击“更多”只会从容器中删除“隐藏内容”类并将链接更改为“更少”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM