我想使用jQuery访问多个元素ID,所以我可以动态地改变这些元素的内容。但是,(我相信)我无法使用第n个子选择器,因为这些元素ID并不全都共享同一父元素,因为它们使用表格布局。如何通过jQuery访问(实际上)元素ID的数组?即#photograph_1#photograph_2
例如,
<table>
<tr>
<td> <div class="bio"><img id="photograph_1" src="test.jpg"></div> </td>
<td> <div class="bio"><img id="photograph_2" src="test.jpg"></div> </td>
</tr>
<tr>
<td> <div class="bio"><img id="photograph_3" src="test.jpg"></div> </td>
<td> <div class="bio"><img id="photograph_4" src="test.jpg"></div> </td>
</tr>
</table>
我以为会有它的ID来访问每个元素的方式,如$('#photograph_1')
但是当我尝试这样做动态这是行不通的。 for(i=0; i++; i<4) { $('#photograph_'+(i+1)).attr('src',image[i]); }
(image []是一组图像源)。
正在尝试做什么?如果是这样,该怎么办?从this reference,我不清楚该怎么做。
不好意思啊,我miscopied我的一些代码,在条件......我包括我的工作了''#码。道歉。 – Eilidh 2013-05-07 13:24:37
应该仍然有效......该代码片段何时运行?控制台中有任何错误? – tymeJV 2013-05-07 13:26:45
它似乎没有工作......没有错误。我只是在'$(document).ready(function(){//在这里运行});' – Eilidh 2013-05-07 13:29:04