捕获href的值并使用它来替换其他href

时间:2016-09-19 12:57:31

标签: javascript jquery

我有两个with open('list.txt') as my_list: for url in my_list: wget.download(url) 元素:

div

我希望从我的<div class="contact-img"> <a href="https://test.de" class="evcms-open-contacts" > <img src="https://cdn-cache.envivo-connect.com/license/Tw3sZjz8v/p/xckYV8bESwsZ/80x80c/xckYV8bESwsZ.png?v=2.4.84-z3p37YEER-1467970717-yZUA33Hxn" alt="F. Alexander Kep"/> </a> </div> <div class="contact-sm"> <a href="https://www.xing.com/profile/FfAlexander"> </div> 课程中获取href,并使用Xing链接替换contact-sm课程的href。因此,如果我点击contact-img,此人的兴页应该会在新窗口中打开。

感谢您的帮助

5 个答案:

答案 0 :(得分:0)

以下代码段可以为您提供帮助。

&#13;
&#13;
$(document).ready(function(){
$('.contact-img a').click(function(){
$(this).attr('href', $('.contact-sm a').attr('href'));
});
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="contact-img">
            <a href="https://test.de" class="evcms-open-contacts" >
              <img src="https://cdn-cache.envivo-connect.com/license/Tw3sZjz8v/p/xckYV8bESwsZ/80x80c/xckYV8bESwsZ.png?v=2.4.84-z3p37YEER-1467970717-yZUA33Hxn" alt="F. Alexander Kep"/>
            </a>
 </div> 
<div class ="contact-sm">
            <a href="https://www.xing.com/profile/FfAlexander">
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

最好的方法是更改​​HTML。但如果你需要&#34;按照你描述的方式进行操作,然后查看这个jQuery代码段:

($(function(){
    $('.contact-img > a').on('click', function(e) {
        e.preventDefault();
        e.stopPropagation();

        $('.contact-sm > a')[0].click();
    })
}))(jQuery);

答案 2 :(得分:0)

&#13;
&#13;
$(function() {
  var xingHref = $(".contact-sm > a").attr("href");
  $(".contact-img > a").attr("href", xingHref);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="contact-img">
  <a href="https://test.de" class="evcms-open-contacts">
    <img src="https://cdn-cache.envivo-connect.com/license/Tw3sZjz8v/p/xckYV8bESwsZ/80x80c/xckYV8bESwsZ.png?v=2.4.84-z3p37YEER-1467970717-yZUA33Hxn" alt="F. Alexander Kep" />
  </a>
</div>
<div class="contact-sm">
  <a href="https://www.xing.com/profile/FfAlexander" />
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

如果在上面的情况下应该这样做:

var desiredHref = $('.contact-sm a').attr('href');
$('.evcms-open-contacts').attr('href', desiredHref);

当然,这可以写成一行,但为了回答。

答案 4 :(得分:-1)

$(document).ready(function(){ $(&#34;#main1&#34;)。attr(&#34; enter code here href&#34;,&#34; https://www.xing.com/profile/FfAlexander&#34;); });

相关问题