Jquery 获取a href attr并将其设置为另一个 $(函数(){ $('.thelink a')。选择(函数(){ var a_href=$(this.attr(“href”); }); $('.calltoActionMoreInformation a').attr('href',a_href); });

Jquery 获取a href attr并将其设置为另一个 $(函数(){ $('.thelink a')。选择(函数(){ var a_href=$(this.attr(“href”); }); $('.calltoActionMoreInformation a').attr('href',a_href); });,jquery,Jquery,正在尝试将href从内部放入: <script type="text/javascript"> $(function() { $('.thelink a').select(function() { var a_href = $(this).attr("href"); }); $('.calltoActionMoreInformation a').attr('href', a_href); }); </script> &l

正在尝试将href从内部放入:

<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>

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


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

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

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

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

更正:

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

是吗?

什么?你的问题是什么?我没有看到问号;-)我如何将.thelink a中的href分配给.calltoActionMoreInformation a?我只能通过单击之类的事件获取href,而不是仅在foreach或anthing中获取它吗?您好,请澄清您的最后一个问题好吗?
$(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
    });

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