我只是想编写一些代码,对于特定类的每个元素,我需要找到某个属性,然后替换一个元素。在代码中,我现在看起来是这样的:帮助与.each.replaceWith()jQuery
<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>"
});
我知道这是不对的,它不工作,但我想不出该怎么写,任何帮助将不胜感激!
EDIT
我应该指出,元素的量不是预定的。
你在插入一个不存在的变量'alt' - 你确定你不是指'hash'吗? – Cameron 2011-04-09 06:53:03
哎呦,是啊让我解决这个问题,这不是问题,只是一个错字。 – mcbeav 2011-04-09 06:55:19