如何检测具有相同ID的点击div文本

如何检测具有相同ID的点击div文本

本文介绍了如何检测具有相同ID的点击div文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $(document).ready(function(){
$ b $('#peoplelayer')。click(function(){
$ (#peoplelayer)。each(function(){

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

});

} );
});

这是代码,我使用相同的ID#peoplelayer给所有的div,当我点击其中的一个,它会以相同的ID提醒所有这个div的文本。

我如何防范这个问题,因为我只需要提醒点击的div,除此之外,我需要给这个divs相同的ID ???

解决方案

ID是唯一的,所以你不能有两个具有相同ID的元素。也许你打算使用类?



divs id ='peoplelayer' > to class ='peoplelayer',您可以使用以下代码:

  $(document).ready(function(){
$ b $('。peoplelayer')。click(function(){
$(this).each(function(){

$(this).fadeOut(500);
var str = $(this).text();
alert(str);
// $( .peoplelayer)。fadeTo(500,0.6);(我知道这是评论,但以防万一)
});
});
});


$(document).ready(function(){

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

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

            });

    });
});

This is code where i use the same id "#peoplelayer" to all the divs, and when i click on one of them, it alerts all the texts of this div with the same id

how can i defend this problem, as i need to alert only the clicked div, in addition to that i need to give this divs the same id ???

解决方案

IDs are unique, so you cannot have two elements with the same IDs. Perhaps you meant to use classes?

Change id = 'peoplelayer' on your divs to class = 'peoplelayer' and you can use this code:

$(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)
            });
    });
});

这篇关于如何检测具有相同ID的点击div文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:10