2015-03-02 66 views
0

我有一堆.post divs,当用户只想查看带有该标签的故事时,它接受.displayOnly的类。我想要做的是为每个帖子显示一条消息,其中显示了“.displayOnly”类,其中显示了消息中的索引号+ 1。这个想法是,这个消息会对“显示3中的1”,“显示3中的2”等等的影响说些什么。我已经尝试过使用index()函数,但对于每个显示“显示1”的消息,我总是得到相同的结果。显示div类的索引()

我在下面列出了我的代码。有人能告诉我我要去哪里吗?

$('h4').click(function() { 
    var numItems = $('.displayOnly').length; 
    var currentItem = $('.displayOnly').index() + 1; 

    if ($('.displayOnly').length > 0) { 
     $("p.itemCount").css('cssText', 'display:block;'); 
     $("p.itemCount").append("Displaying " + currentItem + " of " + numItems + "."); 

    } else { 
     $("p.itemCount").css('cssText', 'display:none;'); 
     $("p.itemCount").empty(); 
    } 
}); 

更新:这里是脚本引用HTML的样本。如果它能够确定长度()我不确定为什么索引(this)不起作用。

<div class="post urbanwildliferefuges displayOnly"> 
 
<p class="date">Tuesday, Feb 24, 2015 - 6:17 PM EST</p> 
 
<h1>Urban Refuges Make it Easier for All People to Get their ‘Nature Fix’</h1> 
 
<p>Debbie Pike is the Visitors Services Manager for Northern New Mexico National Wildlife Refuge Complex (<a href="http://www.fws.gov/refuge/las_vegas/" target="_blank">Las Vegas</a>, <a href="http://www.fws.gov/refuge/rio_mora/" target="_blank">Rio Mora</a> and <a href="http://www.fws.gov/refuge/maxwell/" target="_blank">Maxwell</a> National Wildlife Refuges). “I work with a ‘high energy’ Friends group that&#8230;<a href="http://www.fws.gov/news/blog/index.cfm/2015/1/16/Urban-Refuges-Make-it-Easier-for-All-People-to-Get-their-Nature-Fix" target="_blank">Read more here&#160;&#187;</a> <br/></p> <a href="http://americaswild.tumblr.com/post/111992041452/urban-refuges-make-it-easier-for-all-people-to-get"> 
 
</a><br /> 
 
<h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
<p class="itemCount"></p> 
 
<div style="float:none; clear:both;">&nbsp;</div> 
 
<hr /> 
 
</div> 
 

 
<div class="post urbanwildliferefuges displayOnly"> 
 
<p class="date">Tuesday, Feb 24, 2015 - 6:12 PM EST</p> 
 
<h1>Connection with Nature Can Happen Anywhere, Even in a City</h1> 
 
<p>Georgia Basso is a wildlife biologist in our <a href="http://www.fws.gov/northeast/ecologicalservices/coastal.html" target="_blank">Coastal Program</a> and the Service’s liaison to the <a href="http://longislandsoundstudy.net/" target="_blank">Long Island Sound Study</a>, an EPA National Estuary Program to restore and protect&#8230;<a href="http://www.fws.gov/news/blog/index.cfm/2015/1/12/Connection-with-Nature-Can-Happen-Anywhere-Even-in-a-City" target="_blank">Read more here&#160;&#187; </a><br/></p> <a href="http://americaswild.tumblr.com/post/111991702182/connection-with-nature-can-happen-anywhere-even"> 
 
</a><br /> 
 
<h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
<p class="itemCount"></p> 
 
<div style="float:none; clear:both;">&nbsp;</div> 
 
<hr /> 
 
</div> 
 

 
<div class="post "> 
 
<p class="date">Tuesday, Feb 24, 2015 - 6:06 PM EST</p> 
 
<h1>Secretary Talking about the Urban Wildlife Refuge Program</h1> 
 
<figure class="tmblr-embed" data-provider="youtube" data-orig-width="540" data-orig-height="304" data-url="http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6PaoZViLBPc%26list%3DUUMqPAPfBGsDMs2UiD-5Tbbg"><iframe width="500" height="281" id="youtube_iframe" src="https://www.youtube.com/embed/6PaoZViLBPc?feature=oembed&amp;enablejsapi=1&amp;origin=https://safe.txmblr.com&amp;wmode=opaque" frameborder="0" allowfullscreen=""></iframe></figure> <a href="http://americaswild.tumblr.com/post/111991188777/secretary-talking-about-the-urban-wildlife-refuge"> 
 
</a><br /> 
 
<h4># &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
<p class="itemCount"></p> 
 
<div style="float:none; clear:both;">&nbsp;</div> 
 
<hr /> 
 
</div> 
 

 
<div class="post urbanwildliferefuges displayOnly"> 
 
<p class="date">Tuesday, Feb 24, 2015 - 6:05 PM EST</p> 
 
<h1>14 URBAN WILDLIFE REFUGE PARTNERSHIPS</h1> 
 
<p>From Albuquerque to Yonkers and many cities in between, theU.S. Fish and Wildlife Service is spreading its conservation message. <a href="http://www.fws.gov/refuges/RefugeUpdate/NovDec_2014/14_urban.html" target="_blank">Click here to read more&#160;&#187;</a></p> <a href="http://americaswild.tumblr.com/post/111991128157/14-urban-wildlife-refuge-partnerships"> 
 
</a><br /> 
 
<h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
<p class="itemCount"></p> 
 
<div style="float:none; clear:both;">&nbsp;</div> 
 
<hr /> 
 
</div>

+0

从jquery的文档:'如果没有参数被传递给所述的.index()的方法,所述返回值是指示jQuery对象相对于所述内的第一元素的位置的整数。它的同样的元素.'所以'var currentItem = $('。displayOnly')。index()+ 1;'始终是1. – albciff 2015-03-02 22:23:33

+0

从.index()的jquery文档.....在那里它们使用的示例.. 。(“div”).index(this);尝试在括号之间添加关键字this。 – floor 2015-03-02 22:26:15

+0

'$(“。displayOnly”)。index(this)+ 1' – adeneo 2015-03-02 22:27:00

回答

1

你不设置currentItem到被点击的元素的索引,你将其设置为.displayOnly选择匹配的第一个元素的索引。要参考事件的目标,请使用$(this)。由于displayOnly类在包含DIV上,因此请使用.closest()来查找它。

$(this).closest('div').index('.displayOnly'); 

而且因为有在每一个DIV元素.itemCount,你应该使用.find()只选择在同一个DIV。

$('.displayOnly h4').click(function() { 
 
    var numItems = $('.displayOnly').length; 
 
    var curDiv = $(this).closest('div.displayOnly'); 
 
    var currentItem = curDiv.index('.displayOnly') + 1; 
 

 
    if (numItems > 0) { 
 
    curDiv.find("p.itemCount").show().html("Displaying " + currentItem + " of " + numItems + "."); 
 
    } else { 
 
    curDiv.find("p.itemCount").hide().empty(); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="post urbanwildliferefuges displayOnly"> 
 
    <p class="date">Tuesday, Feb 24, 2015 - 6:17 PM EST</p> 
 
    <h1>Urban Refuges Make it Easier for All People to Get their ‘Nature Fix’</h1> 
 
    <p>Debbie Pike is the Visitors Services Manager for Northern New Mexico National Wildlife Refuge Complex (<a href="http://www.fws.gov/refuge/las_vegas/" target="_blank">Las Vegas</a>, <a href="http://www.fws.gov/refuge/rio_mora/" target="_blank">Rio Mora</a> and 
 
    <a 
 
    href="http://www.fws.gov/refuge/maxwell/" target="_blank">Maxwell</a>National Wildlife Refuges). “I work with a ‘high energy’ Friends group that&#8230;<a href="http://www.fws.gov/news/blog/index.cfm/2015/1/16/Urban-Refuges-Make-it-Easier-for-All-People-to-Get-their-Nature-Fix" target="_blank">Read more here&#160;&#187;</a> 
 
     <br/> 
 
    </p> 
 
    <a href="http://americaswild.tumblr.com/post/111992041452/urban-refuges-make-it-easier-for-all-people-to-get"> 
 
    </a> 
 
    <br /> 
 
    <h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
    <p class="itemCount"></p> 
 
    <div style="float:none; clear:both;">&nbsp;</div> 
 
    <hr /> 
 
</div> 
 

 
<div class="post urbanwildliferefuges displayOnly"> 
 
    <p class="date">Tuesday, Feb 24, 2015 - 6:12 PM EST</p> 
 
    <h1>Connection with Nature Can Happen Anywhere, Even in a City</h1> 
 
    <p>Georgia Basso is a wildlife biologist in our <a href="http://www.fws.gov/northeast/ecologicalservices/coastal.html" target="_blank">Coastal Program</a> and the Service’s liaison to the <a href="http://longislandsoundstudy.net/" target="_blank">Long Island Sound Study</a>, 
 
    an EPA National Estuary Program to restore and protect&#8230;<a href="http://www.fws.gov/news/blog/index.cfm/2015/1/12/Connection-with-Nature-Can-Happen-Anywhere-Even-in-a-City" target="_blank">Read more here&#160;&#187; </a> 
 
    <br/> 
 
    </p> 
 
    <a href="http://americaswild.tumblr.com/post/111991702182/connection-with-nature-can-happen-anywhere-even"> 
 
    </a> 
 
    <br /> 
 
    <h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
    <p class="itemCount"></p> 
 
    <div style="float:none; clear:both;">&nbsp;</div> 
 
    <hr /> 
 
</div> 
 

 
<div class="post "> 
 
    <p class="date">Tuesday, Feb 24, 2015 - 6:06 PM EST</p> 
 
    <h1>Secretary Talking about the Urban Wildlife Refuge Program</h1> 
 
    <figure class="tmblr-embed" data-provider="youtube" data-orig-width="540" data-orig-height="304" data-url="http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D6PaoZViLBPc%26list%3DUUMqPAPfBGsDMs2UiD-5Tbbg"> 
 
    <iframe width="500" height="281" id="youtube_iframe" src="https://www.youtube.com/embed/6PaoZViLBPc?feature=oembed&amp;enablejsapi=1&amp;origin=https://safe.txmblr.com&amp;wmode=opaque" frameborder="0" allowfullscreen=""></iframe> 
 
    </figure> 
 
    <a href="http://americaswild.tumblr.com/post/111991188777/secretary-talking-about-the-urban-wildlife-refuge"> 
 
    </a> 
 
    <br /> 
 
    <h4># &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
    <p class="itemCount"></p> 
 
    <div style="float:none; clear:both;">&nbsp;</div> 
 
    <hr /> 
 
</div> 
 

 
<div class="post urbanwildliferefuges displayOnly"> 
 
    <p class="date">Tuesday, Feb 24, 2015 - 6:05 PM EST</p> 
 
    <h1>14 URBAN WILDLIFE REFUGE PARTNERSHIPS</h1> 
 
    <p>From Albuquerque to Yonkers and many cities in between, theU.S. Fish and Wildlife Service is spreading its conservation message. <a href="http://www.fws.gov/refuges/RefugeUpdate/NovDec_2014/14_urban.html" target="_blank">Click here to read more&#160;&#187;</a> 
 
    </p> 
 
    <a href="http://americaswild.tumblr.com/post/111991128157/14-urban-wildlife-refuge-partnerships"> 
 
    </a> 
 
    <br /> 
 
    <h4>#urbanwildliferefuges &raquo;<br /><span style="font-weight:300; font-style:italic; font-size:.85em !important;">See more like this</span></h4> 
 
    <p class="itemCount"></p> 
 
    <div style="float:none; clear:both;">&nbsp;</div> 
 
    <hr /> 
 
</div>

+0

你可能会用'html'替换'append'。无论如何,这就是我的解决方案的变体:) – Tyblitz 2015-03-02 22:36:29

+0

我并没有真正费心去修复他的代码中的其他可能的错误。 – Barmar 2015-03-02 22:37:16

+0

我已经$(this).index('。displayOnly');以及$(“。displayOnly”)。index(this)+ 1,并且我收到每个帖子的“显示0的3”的结果。不知道我错过了什么。我将包括上面提到的html。 – shepherdMan1016 2015-03-02 23:06:21