Javascript getElementById from php foreach loop


Javascript getElementById from php foreach loop

所以我有一个php foreach循环如下,

$i = 1;
foreach($rows as $record){
       <div id='ytplayer".$i++."'>
       </div>");
}

然后我有使用 getElementById 的 javascript,每次循环时都需要获取div,这本质上是一个数组,但我该怎么做,我知道我在下面的东西不起作用,但像这样的事情,因为我知道这非常接近你如何让它在 ph 中工作

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);
}
document.getElementById('ytplayer'+i);