我有一堆.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…<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 »</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 »<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;"> </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…<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 » </a><br/></p> <a href="http://americaswild.tumblr.com/post/111991702182/connection-with-nature-can-happen-anywhere-even">
</a><br />
<h4>#urbanwildliferefuges »<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;"> </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&enablejsapi=1&origin=https://safe.txmblr.com&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># »<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;"> </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 »</a></p> <a href="http://americaswild.tumblr.com/post/111991128157/14-urban-wildlife-refuge-partnerships">
</a><br />
<h4>#urbanwildliferefuges »<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;"> </div>
<hr />
</div>
从jquery的文档:'如果没有参数被传递给所述的.index()的方法,所述返回值是指示jQuery对象相对于所述内的第一元素的位置的整数。它的同样的元素.'所以'var currentItem = $('。displayOnly')。index()+ 1;'始终是1. – albciff 2015-03-02 22:23:33
从.index()的jquery文档.....在那里它们使用的示例.. 。(“div”).index(this);尝试在括号之间添加关键字this。 – floor 2015-03-02 22:26:15
'$(“。displayOnly”)。index(this)+ 1' – adeneo 2015-03-02 22:27:00