我在显示和隐藏<p>
标签内容时遇到问题,我需要做的是拥有它,因此当页面加载时我的 160 个字符限制为 40 个,然后当您单击显示更多时,它将显示 160 个字符,如果您单击再次单击按钮,您只会看到 40 个字符,我的问题是它会将所有<p>
标签中的所有内容一起显示为每个标签,帮助会很大,在此先感谢!
HTML
<section class="row featured">
<div class="column four comment">
<img src="images/gallery/gallery-1.jpg" alt="">
<div class="content">
<h2>Lorem Ipsum</h2>
<p class="limit">Aliquam dolor odio, porta id mollis ullamcorper, imperdiet sit amet odio. In eleifend dapibus massa ac aliquam. Nullam vulputate lectus ac nunc molestie mollis.</p>
<button class="more align-right"><p class="highlight">Read more</p></button>
</div>
</div>
<div class="column four comment">
<img src="images/gallery/gallery-3.jpg" alt="">
<div class="content">
<h2>Lorem Ipsum</h2>
<p class="limit">Aliquam dolor odio, porta id mollis ullamcorper, imperdiet sit amet odio. In eleifend dapibus massa ac aliquam. Nullam vulputate lectus ac nunc molestie mollis.</p>
<button class="more align-right"><p class="highlight">Read more</p></button>
</div>
</div>
<div class="column four comment">
<img src="images/gallery/gallery-2.jpg" alt="">
<div class="content">
<h2>Lorem Ipsum</h2>
<p class="limit">Aliquam dolor odio, porta id mollis ullamcorper, imperdiet sit amet odio. In eleifend dapibus massa ac aliquam. Nullam vulputate lectus ac nunc molestie mollis.</p>
<button class="more align-right"><p class="highlight">Read more</p></button>
</div>
</div>
</section>
jQuery
$(document).ready(function {
var $this = $(this);
var content = $('.limit').text();
var limit = content.substring(0, 40);
$('.limit').text(limit);
$('.more').click(function() {
var $this = $(this);
var test = $this.siblings('p').text();
var length = test.length;
if(length == 40){
$this.siblings('p').text(content);
}
else {
$this.siblings('p').text(limit);
}
});
});
- 它返回的是“Aliquam dolor odio,porta id mollis ullamcorper,imperdiet sit amet odio。在 eleifend dapibus massa ac aliquam。Nullam vulputate lectus ac nunc Molestie mollis”。每个 p 乘以 3,在此先感谢!