0

我的 Concrete5 模板中有一些代码,如果用户没有上传画廊图像,我不需要这些代码。因此,如果它不包含图像,我需要删除以下代码:

<ul class="Gal_2">
       <li class="Frst">    
       </li>

       <li>
       </li>
</ul>

当它有图像时,它看起来像这样:

<ul class="Gal_2">
    <li class="Frst">
        <p><a rel="group" href="/concrete/files/2113/6352/1788/Lighthouse.jpg"><img width="1024" height="768" alt="Lighthouse.jpg" src="/concrete/files/2113/6352/1788/Lighthouse.jpg"></a>
        </p>
    </li>
    <li>
        <p><a rel="group" href="/concrete/concrete/themes/greek_yogurt/Images/TEMP_IMG1.jpg"><img width="800" height="537" alt="Island Rab" src="/concrete/concrete/themes/greek_yogurt/Images/TEMP_IMG1.jpg" style="float: left;"></a>
        </p>
    </li>
</ul>

检查和删除标记的最快方法是什么?

4

5 回答 5

4

尝试使用:not:has

现场演示

$('.Gal_2:not(:has(a))').remove();
于 2013-03-20T08:55:26.840 回答
2

尝试以下操作:

<?php if (count($images)):?>
<ul class="Gal_2">
    <?php foreach($images as $index => $image):?>
        <li <?php echo $index == 0 ? 'class="Frst"' : ''?>>
            <p>
                <a rel="group" href="<?php echo $image['path']?>">
                    <img width="1024" height="768" src="<?php echo $image['path']?>">
                </a>
            </p>
        </li>
    <?php endforeach?>
</ul>
<?php endif?>

最好在服务器端将其过滤掉,因为用户不需要这些空元素——首先它们必须与 HTTP 请求一起传输,然后必须使用 JavaScript 将其删除。

于 2013-03-20T08:59:31.383 回答
1

你可以这样做:

if (!$('.Gal_2 img').length) { $('.Gal_2').remove(); }

该案例的固定版本有一个带有图像的.Gal_2和另一个没有图像的 .Gal_2:

$('.Gal_2').each(function() {
   if (! $(this).find('img').length) {
      $(this).remove();
   }
});
于 2013-03-20T08:56:07.650 回答
0

你可能会使用类似的东西:

if (!$('img', 'ul.Gal_2 li')) {
    $('ul.Gal_2 li').hide();
    // or
    $('ul.Gal_2 li').remove();
}
于 2013-03-20T08:56:30.500 回答
0

另一个

if($(".Frst").children().length) ? **do smthng** : $(".Gal_2").remove();
于 2013-03-20T09:04:21.703 回答