如何使用ClassName向DIV添加条目

时间:2014-12-03 16:52:15

标签: javascript jquery html

HTML来源:

<span class="specLink">
    <specialty><a title="Plastic Surgery" href="link2.aspx">Plastic Surgery</a></specialty>
</span>
<br />
<span class="specLink">
    <specialty2><a title="Hand Surgery" href="link3.aspx">Hand Surgery</a></specialty2>
</span>

如何创建一个在页面加载期间运行的JQuery脚本,以显示与上面列出的HTML源相同的列表?

E.g:

<div class="justPad">
    <a title="Plastic Surgery" href="link2.aspx" class="defaultLinks">Plastic Surgery</a>
</div>
<div class="justPad">
    <a title="Hand Surgery" href="link3.aspx" class="defaultLinks">Hand Surgery</a>
</div>

我希望如何:

var k = "";
$(".specLink").each(function() {
    var aLink = $(".specLink").replace(<%-- Remove the <specialty#></specialty#> tags and only keep the anchor link --%>);

    k += '<div class="justPad">'; //.. as many entries that shows up
    k += aLink; //.. as many entries that shows up
    k += '</div>'; //.. as many entries that shows up
});

//Once I have added 
$(".addSpecialties").html(k);

空白HTML:

<div class="serviceHolder brClear addSpecialties">
    //add inside here from the JQuery above
</div>

1 个答案:

答案 0 :(得分:1)

类似的东西:

var specialties = $(".specLink a").map(function() {
    return $('<div class="justPad">').append( $(this).clone() )[0];
}).toArray();

$(".addSpecialties").empty().append(specialties);