【发布时间】: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