获取一个href attr并将其设置为另一个

时间:2012-06-13 15:28:20

标签: jquery

<script type="text/javascript">
$(function() {

    $('.thelink a').select(function() {

        var a_href = $(this).attr("href");

    });
    $('.calltoActionMoreInformation a').attr('href', a_href);
});
</script>

  <div class="thelink" style="height: 250px; position: relative;">
  <a title="xxx" href="xxx">
    <img alt="tree" src="x" />

  </a>
  </div>

尝试将href从内部放入:

  <span class="calltoActionMoreInformation" style="position: absolute; bottom: 0px;">
   <a title="" href="--Link here--"></a>
  </span>

如果我设置var a_href ='http://www.google.co.uk';它设置正确,所以问题在于获取.thelink div中唯一链接的href。

如何将.thelink a中的href分配给.calltoActionMoreInformation

3 个答案:

答案 0 :(得分:7)

$('.thelink a').click(function(e) {
   e.preventDefault();
   var a_href = $(this).attr("href"); // or this.href
   $('.calltoActionMoreInformation a').attr('href', a_href);
});

<强> DEMO

答案 1 :(得分:0)

1 - 无法从全局上下文访问变量“a href”,只能访问select函数。

2 - 为什么在这种情况下使用“选择”功能“点击”更合适

更正:

$(function(){

    $('.thelink a').click(function(e) {
        $('.calltoActionMoreInformation > a').attr('href', $(this).attr('href'));
            e.preventDefault(); // or
        return false; // this is important is you dont want the browserfollow the link
    });

});

是不是?

答案 2 :(得分:0)

$('.thelink a').click(function() {
   var a_href = $(this).attr("href");
   $('.calltoActionMoreInformation a').attr('href', a_href);
   return;
});
相关问题