jquery - 比较两个 div 内容并隐藏一个元素
问题描述
如果两个 div 之间的内容相等,我想隐藏要显示的更多文本。如果内容.less-cont
和.more-cont
相等隐藏.more
。请建议
<div class="row">
<div class="col-md-12"><strong>subject</strong></div>
<div class="disp-cont col-md-auto">
Lorem Ipsum is simply dummy text of the printing an
</div>
<div class="more-cont col-md-auto" style="display:none;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
</div>
<a href="#" class="more col-md-auto">read more</a>
<div class="col-md-12">no record</div>
</div>
我在下面尝试过,但没有按预期工作
$( ".row" ).each(function( index ) {
var one = $.trim($(this).closest('.more-cont').text());
var two = $.trim($( this ).closest('.disp-cont').text());
if(one === two){
$(this).closest('.more').hide();
}
});
解决方案
比较text()
每个元素的长度。
$('.row').each(function() {
let _this = $(this),
dispC = _this.children('.disp-cont'),
moreC = _this.children('.more-cont'),
moreBtn = _this.children('.more');
if(moreC.text().length > dispC.text().length){
// add toggle function here
moreBtn.on('click', function(e) {
e.preventDefault();
moreC.slideDown();
moreBtn.hide();
});
} else {
moreC.show();
moreBtn.hide();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h2>Read more example where "more content" has more characters:</h2>
<div class="row">
<div class="col-md-12"><strong>subject</strong></div>
<div class="disp-cont col-md-auto">
Lorem Ipsum is simply dummy text of the printing an
</div>
<div class="more-cont col-md-auto" style="display:none;">
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s
</div>
<a href="#" class="more col-md-auto">read more</a>
<div class="col-md-12">no record</div>
</div>
<h2>Read more example where "more content" is equal to "display content"</h2>
<div class="row">
<div class="col-md-12"><strong>subject</strong></div>
<div class="disp-cont col-md-auto">
Lorem Ipsum is simply dummy text of the printing an
</div>
<div class="more-cont col-md-auto" style="display:none;">
Lorem Ipsum is simply dummy text of the printing an
</div>
<a href="#" class="more col-md-auto">read more</a>
<div class="col-md-12">no record</div>
</div>
<h2>Read more example where "more content" is less than "display content"</h2>
<div class="row">
<div class="col-md-12"><strong>subject</strong></div>
<div class="disp-cont col-md-auto">
Lorem Ipsum is simply dummy text of the printing an
</div>
<div class="more-cont col-md-auto" style="display:none;">
Lorem Ipsum is simply dummy text of
</div>
<a href="#" class="more col-md-auto">read more</a>
<div class="col-md-12">no record</div>
</div>
推荐阅读
- angular - Angular 9服务在登录后和页面重新加载后获取配置
- proxy - 使用 Envoy 在网络之间建立隧道
- python - Python open() 在 w+ 模式下不创建文件
- c++ - C++ 是否可以通过输入执行 cmd 命令?
- python - 将 MultiIndex 转换为单个日期
- javascript - 匹配字符串中的算术运算符,但不匹配负数
- c# - 如果构造函数已参数化,如何使用视图模型初始化 DataContext?
- javascript - 隐藏一个特殊的底部按钮部分
- spring-boot - /dev/stdout 权限被拒绝 Tomcat 访问日志
- javascript - 从 JSON API 请求中获取值并将其显示为网页中的文本