如何在jQuery中动态添加anchor/href

thu*_*nja 8 jquery

我是jQuery的新手,我正在尝试将我的所有手机类转换成带有href的锚:

是)我有的

<div class="phone">111-111-1111</div>
<div class="phone">666-555-4444</div>
Run Code Online (Sandbox Code Playgroud)

我想要的是

<div class="phone"><a href="tel:111-111-1111">111-111-1111</a></div>
<div class="phone"><a href="tel:666-555-4444">666-555-4444</a></div>
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情,但我很遗憾:

$('.phone').each(function(){
  $(this).wrapInner('<a name="???' + $(this).html() + '" />');
});
Run Code Online (Sandbox Code Playgroud)

Vin*_*ins 8

我认为解决方案只在你的问题中...

看看这个.

$('.phone').each(function(){
    $(this).wrapInner('<a href="tel:' + $(this).html() + '" />');
});?
Run Code Online (Sandbox Code Playgroud)

FIDDLE希望这是你想要的.


sco*_*stl 5

$(".phone").each(function(index, element){
    $(element).html($("<a></a>").attr("href", $(element).text()).text($(element).text()));
});
Run Code Online (Sandbox Code Playgroud)