如何为一堆php生成的DIV设置jQuery ID


How to set jQuery ID for bunch of php generated DIVs?

我正在为我的网站使用Lightbox库,jQuery代码是

jQuery(function( $ ) {    
    $('.tosrus a').tosrus({
        pagination : {
            add : true,
            type : "thumbnails"
        },
        buttons : true
    });
});

我的图库将通过PHP生成,每个图库都有不同的图像,如果我使用类来检测图库,它将显示页面中的所有图像。所以我应该使用ID来检测每个库,但问题是我对jQuery一点也不熟悉。

我的PHP代码是

$j = 0;
<div id="tosrus-<?= $j++ ?>" class="tosrus thumbs">  
<a href="<? the_sub_field('images-list'); ?>">
                            <img src="<? the_sub_field('images-list'); ?>"></a>
</div>

因此,对于每个分区,结果将类似于tosrus-1, tosrus-2

有人知道我该怎么做吗?

您不必专门指定一个索引id来与特定索引处的项交互,而是可以使用:nth of type选择器。

例如:

$("div:nth-of-type(1)").hide();

此处的示例:http://jsbin.com/ciraruhuhimi/1/edit?html,js,输出

从Jquery选择器属性包含前缀选择器,您可以获得div元素

$( "div[id|='tosrus']" )

此选择器用于获取具有指定属性的元素,该属性的值等于给定字符串或以该字符串开头并后跟连字符(-)。