获取“id”并添加每个div(name =“id”)
$(document).ready(function() { var idPost = $('.post-outer').attr('id'); $('.post-outer').attr('name', idPost); });
当你在上面运行JS时,它只需要在其他“DIV”中重复第一个“ID”。 到底是怎么回事:
teste1 teste2 teste3
我想发生这样的事:
teste1 teste2 teste3
演示
$('.post-outer .post-footer .post-footer-line.post-footer-line-2 .post-labels a').each(function(index, el) { $(this).parents('.post-outer:eq(0)').attr('itemtype', $(this).attr('href') ); });
使用.each()
方法:
$('.post-outer').each(function() { $(this).attr( 'name', $(this).attr('id') ); });
迭代jQuery对象,为每个匹配的元素执行一个函数。
.attr()
函数可以将函数作为参数。 它为每个元素重复调用该函数(如.each()
),返回的值是新的属性值。
$('.post-outer').attr('name', function() { return this.id; });
这基本上等同于mevius的答案,但它不会反复调用.attr()
。