jQuery-将动态链接从一个元素复制到另一个 [英] jQuery - Copy a dynamic link from one element to another

查看:54
本文介绍了jQuery-将动态链接从一个元素复制到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

div.alpha中的URL是动态的.我需要使用此URL,并将其应用于包装了div.beta中视图详细信息"的"a href"

The URL in div.alpha is dynamic. I need to take this URL and apply it to an 'a href' that wraps 'View Details' in div.beta

<div class="alpha">
    <a href="http://www.url.com">Example</a>
    Plus some other stuff which may contain links, etc
    that I don't want to copy to the other div.
</div>

<div class="beta">
    View Details
</div>

所需结果:

<div class="alpha">
    <a href="http://www.url.com">Example</a>
    Plus some other stuff which may contain links, etc
    that I don't want to copy to the other div.
</div>

<div class="beta">
    <a href="http://www.url.com">View Details</a>
</div>

我可以这样获得链接:$('.alpha').find('a').attr('href');

I can get the link like this: $('.alpha').find('a').attr('href');

但是我不知道如何将该链接传递到下面的XXXX:

But I don't know how to pass that link to the XXXX below:

$('.beta').each(function(){
    $(this).text($(this).text().replace('View Details', '<a href="XXXX">View Details</a>'));
});

推荐答案

您可以克隆a.alpha,然后用它包装.b的内容:

You could clone a.alpha, then just wrap .b's contents with it:

// clone the link, remove the text
$link = $('.alpha').find('a').clone().text('');

// wrap all .beta's contents with the cloned link
$('.beta').contents().wrap($link);

这篇关于jQuery-将动态链接从一个元素复制到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆