0

I have some code in my Concrete5 template which i dont need if the user has not uploaded gallery images. I therefore need to remove the following code IF it contains no images:

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

       <li>
       </li>
</ul>

It looks like this when it has images in:

<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>

What would be the fastest way to check and remove the markup?

4

5 に答える 5

4

Try using :not and :has

Live Demo

$('.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

もう1つ

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

おそらく次のようなものを使用するでしょう:

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