我正在尝试根据点击折叠和扩展div。具有以下代码的东西工作正常,但容器始终在页面加载时保持扩展。所以我想做相反的事情。当页面加载时,它应该被折叠。
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;
});
<?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; ?>
请帮我解决一下!
<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>
答案 0 :(得分:0)
让我觉得你的代码比你可能需要的代码多得多。
以下是我认为您的目标设置的简化版本:
$(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();
});
});
&#13;
.toggle {
color: rgb(0,0,255);
text-decoration: underline;
cursor: pointer;
}
&#13;
<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>
&#13;