如何使用ClassName将条目添加到DIV [英] How to use ClassName add entries to a DIV
本文介绍了如何使用ClassName将条目添加到DIV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML来源:
< span class =specLink>
<专业>< a title =整容手术href =link2.aspx>整容手术< / a>< / specialty>
< / span>
< br />
< span class =specLink>
< specialty2>< a title =手术href =link3.aspx>手部手术< / a>< / specialty2>
< / span>
如何创建一个在页面加载期间运行的JQuery脚本来显示从HTML例如:
< div class = justPad >
< / div>
< div class =justPad>
< a title =手术href =link3.aspxclass =defaultLinks>手部手术< / a>
< / div>
我希望如此:
var k =;
$(。specLink)。each(function(){
var aLink = $(。specLink)。replace(<% - 移除<专业#>< / speciality#>标记,并且只保留锚链接 - %>);
k + ='< div class =justPad>'; // ..尽可能多的条目显示出
k + = aLink; // ..显示
k + ='< / div>'; // ..的条目数量显示
}的条目数量) ;
//一旦我添加了
$(。addSpecialties)。html(k);
空白HTML:
< div class =serviceHolder brClear addSpecialties>
//在
< / div>之上的JQuery里面添加
解决方案
类似于:
var specialties = $(。specLink a)。map(function(){
return $('< div class =justPad >')。append($(this).clone())[0];
})。toArray();
$(。addSpecialties)。empty()。append(specialties);
HTML source:
<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>
How can I create a JQuery script which runs during page load to displays the same list taking from the HTML Source listed above?
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>
How I would like it to be:
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);
Blank HTML:
<div class="serviceHolder brClear addSpecialties">
//add inside here from the JQuery above
</div>
解决方案
Something like:
var specialties = $(".specLink a").map(function() {
return $('<div class="justPad">').append( $(this).clone() )[0];
}).toArray();
$(".addSpecialties").empty().append(specialties);
这篇关于如何使用ClassName将条目添加到DIV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文