我只需要选择一个条件为true的文章。在代码中,我使用hourDiff == 0作为真实条件。我正在用它们来遍历文章。在CSS中,我有display:none的文章。该代码中的问题是,在满足条件之前,所有文章都会出现。
<section class="noticias">
<article class=" article element" iteridart="FL2049412">
<script type="text/javascript"> hourDiff== 2 </script>
</article>
<article class=" article element" iteridart="FL2049417">
<script type="text/javascript"> hourDiff== 0 </script>
</article>
<article class=" article element" iteridart="FL2045411">
<script type="text/javascript"> hourDiff== 4 </script>
</article>
</section>
function templateXXX {
$(".top-right-col .noticias article").each(function(index, el){
if(hourDiff == 0) {
$( el ).addClass( "showClass" );
}
});
}
我认为您正在错误地解决此问题,但是作为一种变通办法(可以在脚本标记中搜索文本“ hourDiff == 0”,您可以这样做:]]
https://jsfiddle.net/pqjd2h5a/2/
$(".article").each(function(index, el){ console.log(index); console.log(el); if($(this).text().includes("hourDiff== 0")) { console.log("found"); $(this).append( "<p>hourDiff== 0</p>" ) $(this).addClass("showClass"); } });
理想情况下,您不应将脚本标签作为文章元素的子级。是否可以将hourDiff作为值添加到每篇文章或类似文章中?