开发者

help with .each.replaceWith() jQuery

I am just trying to write some code, for each element of a specific class i need to find a certain attribute, then replace an element. the code i have right now lo开发者_运维百科oks like this:

<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>"
});

i know this is not right, and its not working, but i can't think of how to write this, any help would be greatly appreciated!

EDIT

i should mention that the amount of elements is not predetermined.


I think you want following:

$('.a-photo').each()(function(){
    $(this).css({'background':'url('+$(this).attr('alt') + ') center no-repeat'});
});


You are using each().replaceWith which is an error and also superfluous becaue replaceWith already does the same. Try:

$('.a-photo').replaceWith(function(){
  var alt = $(this).attr('alt');
  return "<div class='a-photo' style='background:url("+ alt +") center no-repeat;></div>";
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜