gpt4 book ai didi

javascript - jquery仅在一个类中显示更多功能目标p标签

转载 作者:行者123 更新时间:2023-12-04 08:24:47 24 4
gpt4 key购买 nike

显示更多功能是错误的 P 标记,我不能在 p 标记中使用类,因为它来自数据库,所以我如何在类 contentclass 中定位 P 目标?

$(document).ready(function(){
if ($('p').length > 1) {
$('p:gt(1)').hide('slow');
$('.show-more').show('slow');
}

$('.show-more').on('click', function() {
$('p:gt(1)').slideToggle('slow');
$(this).html() == 'ReedMore +' ? $(this).html('Less -') : $(this).html('ReedMore +');
});


});
    .show-more {
display: none;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="contentclass">
<p>Testing some of Text</p>
<p>SecondTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<div class="show-more">ReedMore +</div>
</div><!-- <div class="contentclass"> -->

<!-- i Dont Want be Target the next P teags -->
<p style="color:red;">Testing Testing Confirm its target Only First Pargraph</p>
<p style="color:red;">Testing Testing Confirm its target Only First Pargraph</p>

最佳答案

由于您直接使用了p,因此您没有具体指定应用更改的位置。作为选择器,因此它对所有 p 进行更改在您的 html 中添加标签,这样您就可以添加 contentclassp标签 。
演示代码 :

//now only p tags which is inside content will get change
if ($('.contentclass p').length > 1) {
$('.contentclass p:gt(1)').hide('slow');
$('.show-more').show('slow');
}

$('.show-more').on('click', function() {
$('.contentclass p:gt(1)').slideToggle('slow');
$(this).html() == 'ReedMore +' ? $(this).html('Less -') : $(this).html('ReedMore +');
});
.show-more {
display: none;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="contentclass">
<p>Testing some of Text</p>
<p>SecondTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<p>ThirdTesting some of Text</p>
<div class="show-more">ReedMore +</div>
</div>

<p style="color:red;">Testing Testing Confirm its target Only First Pargraph</p>
<p style="color:red;">Testing Testing Confirm its target Only First Pargraph</p>

关于javascript - jquery仅在一个类中显示更多功能目标p标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65317454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com