帮助 .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>"
});
我知道这是不对的,而且它不起作用,但我想不出如何写这个,任何帮助将不胜感激!
编辑
我应该提到元素的数量不是预先确定的。
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 looks 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你想要以下内容:
I think you want following:
您正在使用
each().replaceWith
这是一个错误,也是多余的,因为replaceWith
已经做了同样的事情。尝试:You are using
each().replaceWith
which is an error and also superfluous becauereplaceWith
already does the same. Try: