我有一个PHP while循环,我动态生成#id。
示例:
<?php
$i=1;
while ($row = mysqli_fetch_array($result)):
?>
<tr>
<td class='hashtag' id='status<?php echo $i; ?>'>status value</td>
</tr>
<?php
$i++;
endwhile:
?>
状态ID生成如下:status1,status2,status3等......
我想在我的JS代码ON LOAD中将所有这些Id用于将其显示到模态对话框中。
示例:
<script type="text/javascript">
$(document).ready(function(){
$("#status1");
$("#status2");
$("#status3");
.
.
and so on as per the php while loop.
});
</script>
我怎样才能做到这一点?
答案 0 :(得分:3)
您可以使用&#34; Starts With&#34;选择器:
$('td[id^="status"]').each(function(index){
alert($(this).attr("id"));
});
您可以指定选择器的范围,以便定位主要的td&#39;
答案 1 :(得分:1)
我会这样做:
更改您的HTML
<td class='hashtag status'>status value</td>
然后js看起来像这样:
$('.status').each(function(i, el){
// the i value previously generated in php is here i += 1 if you need it
i += 1;
// here is your element. do whatever you want with it:
$(el).text('I am cell ' + i);
});
答案 2 :(得分:1)
$("td[id^='status']").each(function(el){
console.log(this.id);
});
这将为您提供每个元素的ID。
如果您只想申请活动或插件,可以通过 -
进行$("td[id^='status']").pluginName();
或
$("td[id^='status']").on("click",function(){
});