Javascript 设置显示:如果元素不包含文本,则为“无”
我想检查.pp post content awards和pp post content location中是否没有文本 如果未设置显示:两个元素以及之前的元素均为“无”,这两个元素都为.pp post content subtitle DOM中有几个这样的实例,但只想删除空的实例和它前面的元素,即副标题 我知道我必须使用.each(),但不太清楚如何使用Javascript 设置显示:如果元素不包含文本,则为“无”,javascript,jquery,html,Javascript,Jquery,Html,我想检查.pp post content awards和pp post content location中是否没有文本 如果未设置显示:两个元素以及之前的元素均为“无”,这两个元素都为.pp post content subtitle DOM中有几个这样的实例,但只想删除空的实例和它前面的元素,即副标题 我知道我必须使用.each(),但不太清楚如何使用 <div id="pp-post-right-{post_id}" class="pp-post-right"> <
<div id="pp-post-right-{post_id}" class="pp-post-right">
<span class="pp-post-content-subtitle">Location</span>
<div class="pp-post-content-location">{%ptb_location%}</div>
<span class="pp-post-content-subtitle">Accolades & Awards</span>
<div class="pp-post-content-awards">{%ptb_accolades_and_awards%}</div>
</div>
$('.pp-post-content-location').each(function() {
let locationContent = $(''this').text();
if (locationContent = '') {
$('this').css('display','none');
}
});
$('.pp-post-content-awards').each(function() {
let awardsContent = $('this').text();
if (awardsContent = '') {
$('this').css('display','none');
}
});
地方
{%ptb_位置%}
嘉奖及;奖品
{%ptb_获得_奖和_奖%}
$('.pp post content location')。每个(函数(){
让locationContent=$(“this”).text();
如果(位置内容=“”){
$('this').css('display','none');
}
});
$('.pp post content awards')。每个(函数(){
let awardsContent=$('this').text();
如果(奖励内容=“”){
$('this').css('display','none');
}
});
您可以使用选择器
选择没有子元素的所有元素(包括文本节点)
实现中的问题是使用
此引用迭代中的当前元素,因此不应在引号中,=
是赋值运算符,您需要使用=
/==
进行比较
$('.pp-post-content-location').each(function() {
let locationContent = $(this).text();
if (locationContent == '') {
$(this).css('display','none');
}
});
let locationContent=$(“this”).text()
=>var locationContent=$(this).text()代码>
$('.pp post content location')。每个(函数(){
var locationContent=$(this.text();
如果(位置内容=“”){
$(this.css('display','none');
}
});
$('.pp post content awards')。每个(函数(){
var awardsContent=$(this.text();
如果(奖励内容=“”){
$(this.css('display','none');
}
});代码>
地方
嘉奖及;奖品
{%ptb_获得_奖和_奖%}
地方
{%ptb_位置%}
嘉奖及;奖品
{%ptb_获得_奖和_奖%}
$('.pp post content location')。每个(函数(){
让locationContent=$('this').text();//更正此错误
如果(locationContent=''){//更正了此错误
$('this').css('display','none');
}
});
$('.pp post content awards')。每个(函数(){
let awardsContent=$('this').text();
如果(awardsContent=''){//更正了此错误
$('this').css('display','none');
}
});
=用于分配
==线性等式
==严格相等
$('.pp-post-content-awards').each(function(){
if (!$(this).text().trim().length) {
$(this).css('display','none');
}});
用你的代码尝试了这个,并且成功了。使用Jquery函数trim并获取长度,以确保它不仅包含空格。请参阅以下代码段:
$(".pp-post-right").each(function(){
str1 = $.trim($(".pp-post-content-awards").html()).length;
str2 = $.trim($(".pp-post-content-location").html()).length;
if(str1 == 0 && str2 == 0)
{
$(this).find(".pp-post-content-subtitle,.pp-post-content-location,.pp-post-content-awards").hide();
}
});
$('.pp post content location li')。每个(函数(){
让locationContent=$(this.text();
console.log(locationContent)
如果(位置内容=“”){
$(this.css('display','none');
}
});
$('.pp post content awards')。每个(函数(){
let awardsContent=$('this').text();
如果(奖励内容=“”){
$('this').css('display','none');
}
});代码>
地方
美国
美国
美国
美国
嘉奖及;奖品
如果前面的元素为空,我将如何隐藏它前面的元素?@gcollins$('.pp post content location:empty').prev().hide()
作为旁注,您应该以不同的方式构建HTML。你不应该让你的标题和副标题以这样的方式到处乱跳——它们都应该被包装在自己的div中。这样你就不必问“如何隐藏一个空元素和它前面的元素”——你只需要隐藏一个元素,其中包含标题和副标题。
$('.pp-post-content-awards').each(function(){
if (!$(this).text().trim().length) {
$(this).css('display','none');
}});
$(".pp-post-right").each(function(){
str1 = $.trim($(".pp-post-content-awards").html()).length;
str2 = $.trim($(".pp-post-content-location").html()).length;
if(str1 == 0 && str2 == 0)
{
$(this).find(".pp-post-content-subtitle,.pp-post-content-location,.pp-post-content-awards").hide();
}
});