3

我有一个 div 加载每页上的图像数量。例如:

<div class="totalimages">1</div>
<div id="arrows">NEXT</div>

当我有一张图片时,我想隐藏#arrows div。我对 jQuery 不是很好,所以我想知道它的语法是什么,

如果 .totalimages div 等于“1”,则隐藏 div #arrows。

谢谢!

4

3 回答 3

8

试试这个:

if ($(".totalimages").text() == "1") {
    $("#arrows").hide();
}

或在一行中:

$("#arrows").toggle($(".totalimages").text() != "1");
于 2012-10-29T10:12:16.653 回答
1

试试这个

if($('.totalimages').html() == 1){
    $("#arrows").hide();
}​

这是小提琴:http: //jsfiddle.net/paw5w/15/

于 2012-10-29T10:15:49.383 回答
0

我可以建议您将演示文稿与数据分离吗?使用新的 HTML5 属性,您可以以更简洁的方式实现此目的,而无需依赖脚本的演示文稿。这允许您更改显示图像总数的方式,并且仍然达到相同的效果,即隐藏箭头:

<div class="totalimages" data-total-images="1">1</div>
<div id="arrows">NEXT</div>

var totalImages = parseInt($('.totalimages').data('totalImages'), 10);
if(totalImages <= 1) {
  $('#arrows').hide();
}

如您所见,我添加了一个data--attribute,其中包含您感兴趣的数据。现在,您可以在totalimages不破坏脚本的情况下更改 -div 的文本:

<div class="totalimages" data-total-images="15">Showing image 1 out of 15</div>
于 2012-10-29T10:20:02.617 回答