从数组填充href

时间:2014-12-17 19:13:03

标签: javascript jquery

我试图找到一种方法来填充链接的href"暴露"使用" titel"。

的href

该函数正确选择href并将其写入控制台,但公开的href始终是数组的最后一个元素。但它需要正确排序

  e1标题为e1暴露   e2 titel到e2暴露   ...

<div class="e1">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>
<div class="e2">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>
<div class="e3">
    <div class="titel"><a href="test.html">titel</a></div>
    <div class="expose"><a href="">expose</a></div>
</div>

$('.titel a').each(function() {
      var ary= $(this).attr("href");
      console.log(ary);

      $('.expose a').each(function() {          
         $(this).attr("href", "what should be written in front of the link"+ (ary) +"download.pdf");            
    });     
});

2 个答案:

答案 0 :(得分:2)

这应该这样做:

$('.titel a').each(function() {
    var ary = $(this).attr("href");
    $(this).parent().next().find('a').attr('href', ary);
});

答案 1 :(得分:2)

你的问题是内循环。而不是那个循环,做这样的事情:

$('.titel a').each(function() {
    var ary= $(this).attr("href");
    console.log(ary);

    $(this).parent().find('.expose a').attr("href", "what should be written in front of the link"+ (ary) +"download.pdf");
});