$(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/