【问题标题】:jQuery Read More container expanding issuejQuery Read More 容器扩展问题
【发布时间】:2017-05-15 21:13:51
【问题描述】:

我正在尝试根据点击折叠和展开 div。以下代码运行良好,但容器始终在页面加载时保持扩展。所以我想做相反的事情。当页面加载时,它应该是折叠的。

jQuery

var showTotalChar = 200, showChar = "read more", hideChar = "less";
$('.review_item').each(function () {
    var content = $(this).html();

    if (content.length > showTotalChar) {
        var con = content.substr(0, showTotalChar);
        var hcon = content.substr(showTotalChar, content.length - showTotalChar);
        var txt = con + '<span class="morectnt"><span>' + hcon + '</span><span class="read-more sample"><a href="" class="clickme">' + hideChar + '</a></span>';
        $(this).html(txt);
    }
});
$(".clickme").click(function () {
    if ($(this).hasClass("sample")) {
        $(this).removeClass("sample");
        $(this).text(hideChar);
    } else {
        $(this).addClass("sample");
        $(this).text(showChar);
    }
    console.log($(this).parent().prev());
    $(this).parent().prev().toggle();
    $(this).prev().toggle();
    return false;
});

HTML/PHP

<?php
foreach ( $product[ 'review' ] as $review ): ?>
    <div class="greycont">
        <img src="<?php echo $review[ 'image_url' ] ?>"
             alt="<?php echo $review[ 'customer' ] ?>">
        <h2><?php echo $review[ 'review_heading' ] ?></h2>
        <div class="null"></div><!-- this div is just to avoid read more issue -->
        <div class="review_item">
            <?php //echo $review[ 'review_content' ] ?>
            <?php eWC_text_to_paragraph( $review[ 'review_content' ] ) ?>
        </div>
<!--                                <span class="read-more"><a href="#" class="clickme">read more...</a></span>-->
    </div><!-- end div class greycont -->
<?php endforeach; ?>

请帮我解决它!

更新

添加呈现的 HTML

<div class="greycont">
   <img src="http://localhost/swedencare/wp-content/uploads/2017/01/blogimg.jpg" alt="Elle Dicosta">
   <h2>This is an amazing product</h2>
   <div class="null"></div>
   <!-- this div is just to avoid read more issue -->
   <div class="review_item">
      <p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they <span class="morectnt"><span>live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.
         </span></span>
      </p>
      <p></p>
      <p>It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.</p>
      <p></p>
      <p>The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek.</p>
      <span class="read-more sample"><a href="" class="clickme">less</a></span>
   </div>
   <!--                                <span class="read-more"><a href="#" class="clickme">read more...</a></span>-->
</div>

【问题讨论】:

  • 您有任何可以展示的工作示例吗?也请检查控制台是否有任何错误。
  • 不幸的是,我在本地端。我在发布之前检查了控制台日志,并且没有错误。该代码工作正常,但以相反的方式工作。意味着当页面加载时它总是展开,但我想在加载时它应该被折叠。
  • 请分享一个示例标记。 PHP 在这里并没有太大帮助,因为 MCVE 需要一些示例输出。
  • css: .review_item { display:none } 并且请不要将任何内容称为 null
  • @mplungjan 感谢您的回复,但它隐藏了整个容器并且没有显示任何内容

标签: javascript php jquery html css


【解决方案1】:

令我震惊的是,您的代码比您可能需要的要多得多。

这是我认为您的目标设置的简化版本:

$(document).ready(function(){
    
    $('.review_item p:nth-of-type(3)').slideToggle();
    $('.toggle').eq(1).toggle();

    $('.toggle').click(function(){
        $('.review_item p:nth-of-type(3)').slideToggle();
        $('.toggle').toggle();
    });

});
.toggle {
color: rgb(0,0,255);
text-decoration: underline;
cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="review_item">
<p>Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>

<p>It is a paradisematic country, in which roasted parts of sentences fly into your mouth. Even the all-powerful Pointing has no control about the blind texts it is an almost unorthographic life One day however a small line of blind text by the name of Lorem Ipsum decided to leave for the far World of Grammar.</p>

<p>The Big Oxmox advised her not to do so, because there were thousands of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind Text didn’t listen. She packed her seven versalia, put her initial into the belt and made herself on the way. When she reached the first hills of the Italic Mountains, she had a last view back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet Village and the subline of her own road, the Line Lane. Pityful a rethoric question ran over her cheek.</p>

<span class="toggle">Read More</span>
<span class="toggle">Read Less</span>
</div>

【讨论】:

  • 感谢您提供的出色建议和代码示例。我在代码中有一个小问题是我的内容对于每个评论项目没有相同数量的段落。你能指导我如何编写 jQuery 以便不管段落数如何都能正常工作吗?
  • 您要切换哪些段落?是否总是从第 3 段开始的所有段落?
猜你喜欢
  • 2019-09-16
  • 2010-12-25
  • 2012-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多