使用jquery';s each()将单击事件添加到imgs

使用jquery';s each()将单击事件添加到imgs,jquery,Jquery,我使用jquery的each()将单击事件添加到一组img中,这很好,但我希望每个img对单击有不同的影响 $('#worksfoot img').each( function() { $(this).click( function() { $('#entrycontainer').animate({ marginLeft: },500); }) }) 我希望第一个img将marginLeft设置为0,然后将其他img

我使用jquery的each()将单击事件添加到一组img中,这很好,但我希望每个img对单击有不同的影响

$('#worksfoot img').each( function() {

    $(this).click( function() {



        $('#entrycontainer').animate({
        marginLeft:  

        },500); })



})

我希望第一个img将marginLeft设置为0,然后将其他img的值增加100。

您可以尝试以下解决方案:

$('#worksfoot img').each( function(index) {
    $(this).click( function() {
        $('#entrycontainer').animate({
            marginLeft: 100*index
        },500);
    });
});

您不应该对不同的元素使用多个id。
要使其正常工作,请始终尝试使用类而不是id。

不起作用,尽管看起来应该这样做。当我将警报(索引)添加到click事件时,我没有看到对话框。同时,它冻结了同样为entrycontainer设置动画的按钮
$('#worksfoot img').each( function(index) {

    $(this).click( function() {

        $('#entrycontainer').animate({
             marginLeft: index * 100 

        },500); })



})
('#worksfoot img').each( function(index, elem) {
    elem.click( function() {
        $('#entrycontainer').animate({
        marginLeft:  100*index
        },500); })
})