0

我的网站上有一个轮播,如果没有元素,我的 jquery 似乎会崩溃。

我没有元素的 HTML 输出是......

<section class="gallery-viewport-twobedroomapartment viewer">
     <ul>
     </ul>
     <a id="simplePrevious"><img alt="Left Arrow" src="_includes/images/larr.png"></a>
     <a id="simpleNext"><img alt="Right Arrow" src="_includes/images/rarr.png"></a>
</section>

我的 jQuery 是...

$('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');

是否可以说如果 UL 包含 LI 然后运行该功能?

4

4 回答 4

1
if($('.gallery-viewport-twobedroomapartment ul > li').length > 0) {
   $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
}
于 2012-09-18T10:26:08.857 回答
1

要知道 jQuery 集合中是否有任何元素,请使用length.

if ($(elements).length) {
  // there are elements
}
于 2012-09-18T10:33:38.060 回答
0
var liCount = $('ul li').length;

if (liCount > 0) {
  $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
}

繁荣!

于 2012-09-18T10:25:57.800 回答
0

您可以使用.children()length

if ($(".gallery-viewport-twobedroomapartment").children("ul").children("li").length > 1)
    $('.gallery-viewport-twobedroomapartment').carousel('#simplePrevious', '#simpleNext');
于 2012-09-18T10:26:39.137 回答