我只是想写一些代码,对于特定类的每个元素,我需要找到某个属性,然后替换一个元素。我现在拥有的代码如下所示:
<div class="a-photo" alt="*/THE IMG SRC*/"></div>
<div class="a-photo" alt="*/THE IMG SRC*/"></div>
<div class="a-photo" alt="*/THE IMG SRC*/"></div>
<div class="a-photo" alt="*/THE IMG SRC*/"></div>
$('.a-photo').each().replaceWith(function(){
var hash = $(this).attr('alt')
"<div class='a-photo' style='background:url("+ hash +") center no-repeat;></div>"
});
我知道这是不对的,而且它不起作用,但我想不出如何写这个,任何帮助将不胜感激!
编辑
我应该提到元素的数量不是预先确定的。