我试图在单击时提醒处于 for 循环中的 div 的文本。有很多具有相同 id 的 div,如果单击第 3 个,我想获得 3 个,或者单击第 2 个时获得 2 个。我怎样才能用下面的代码来实现它或者我必须添加什么?谢谢。
<? for($i=0; $i<5; $i++) { ?>
<div id="abc"><? echo $i ?></div>
<? } ?>
<script>
$(function() {
$("#abc").click(function() {
var thevar= $("#abc").text();
alert (thevar);
}
</script>
最佳答案
只需尝试在循环内使用 class
而不是使用 id
,
<? for($i=0; $i<5; $i++) { ?>
<div class="abc"><? echo $i ?></div>
<? } ?>
<script>
$(function() {
$(".abc").click(function()
{
var thevar= $(this).text();
alert (thevar);
});
});
</script>
关于javascript - 如何从php中的for循环中获取jquery值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23238548/