javascript - 如何检测具有相同 ID 的已单击 div 文本

标签 javascript jquery html

$(document).ready(function(){

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

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

            });

    });
});

这是我对所有 div 使用相同 ID“#peoplelayer”的代码,当我单击其中一个时,它会提醒该 div 具有相同 ID 的所有文本

我该如何解决这个问题,因为我只需要提醒点击的 div,此外我还需要给这个 div 相同的 id ???

最佳答案

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

div 上的id = 'peoplelayer' 更改为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)
            });
    });
});

关于javascript - 如何检测具有相同 ID 的已单击 div 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20955810/

相关文章:

javascript - 更改 navigator.notification.confirm 背景颜色(白色到黑色)

javascript - Webpack-即使在 npm 安装后也找不到模块

php - 如何将 Javascript 中的元素 ID 转换为 jQuery?

css - 非 JS 方法停止 <input> 中的 "enter"提交表单?

javascript - expressJS 路由器规划 - 如何设置有效的路由器

html - 如何在嵌套的 div 中对齐 h2?

javascript - 如何在 react 中使用外部对象?

javascript - 使用 $(document) 仅调用一次脚本

javascript - Jquery 中的 Require 不是未定义吗?

javascript - 我们如何使用js获取循环表中的隐藏值?