如何使用相同的ID检测单击的div文本

Abd*_*ter 1 html javascript jquery

$(document).ready(function(){

    $('#peoplelayer').click(function(){
        $("#peoplelayer").each(function(){

            $("#peoplelayer").fadeOut(500);
            var str = $(this).text();
            alert(str);
            //$("#peoplelayer").fadeTo(500,0.6);

            });

    });
});
Run Code Online (Sandbox Code Playgroud)

这是代码,我对所有div使用相同的id"#peoplelayer",当我点击其中一个时,它会以相同的id警告这个div的所有文本

我怎么能防止这个问题,因为我需要提醒只有被点击的div,除了我需要给这个divs相同的id ???

Cil*_*lan 10

ID是唯一的,因此您不能拥有两个具有相同ID的元素.也许你打算使用课程?

更改id = 'peoplelayer'您的div来class = 'peoplelayer',你可以使用此代码:

$(document).ready(function(){

    $('.peoplelayer').click(function(){
        $(this).each(function(){

            $(this).fadeOut(500);
            var str = $(this).text();
            alert(str);
            //$(".peoplelayer").fadeTo(500,0.6); (I know it's commented, but just in case)
            });
    });
});
Run Code Online (Sandbox Code Playgroud)