所以我有一个 php foreach 循环,如下所示,
$i = 1;
foreach($rows as $record){
<div id='ytplayer".$i++."'>
</div>");
}
然后我有使用getElementById
的javascript,它需要在每次循环时获取div,这本质上是一个数组,但我该怎么做,我知道我有什么下面是行不通的,但是像这样的东西,因为我知道这非常接近你在 php 中的工作方式
document.getElementById('ytplayer[i]');
最佳答案
给你!
http://jsfiddle.net/epinapala/5yUAx/
假设你的 PHP 编写了以下 div(例如添加一个、两个等......)
<div id='ytplayer1'>one</div>
<div id='ytplayer2'>two</div>
<div id='ytplayer3'>three</div>
<div id='ytplayer4'>four</div>
for(var i=1; i<5; i++){
var x=document.getElementById('ytplayer'+i);
alert(x.innerHTML);
}
关于php - php foreach 循环中的 Javascript getElementById,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092909/