我有一个HTML元素,其中包含包含作者文本名称的跨度列表,我需要将每个作者名称范围分别放在li元素列表中,如何使用JQuery执行此操作?
var all_author_names = $('.postcontent .entry_content #author-name'); // get list of spans containg names
var li_to_modifiy = $('.entry_meta > li:nth-child(3)') // gets list of li elements that I need to add each name respectively
修改
简化的HTML:
<div class="postcontent nobottommargin clearfix">
<ul class="entry_meta clearfix">
<li class="hide"></li>
<li></li>
<li><span>|</span><a href="#"><i class="icon-link"></i>Permalink</a></li>
</ul>
<span class="page-divider"><span></span></span>
<div class="entry_content">
<p></p><div class="entry_image">
<span id="author-name" class="hide">Karen Hutchins</span>
</div>
</div>
</div>
注意:postcontent类是自动生成的,我有很多,不仅仅是一个。
说清楚:
1- let all_author_names包含此html:
<span>my name</span>
<span>other name</span>
<span>your name</span>
2-我需要postcontent类中的每个li元素都有一个与其索引相关的span,所以结果是:
<div class="postcontent nobottommargin clearfix">
<ul class="entry_meta clearfix">
<li class="hide"></li>
<li><span>my name</span></li>
其他postcontent类
<div class="postcontent nobottommargin clearfix">
<ul class="entry_meta clearfix">
<li class="hide"></li>
<li> <span>other name</span></li>
答案 0 :(得分:1)
但我想,你想要这样的东西:
var all_author_names = $('.postcontent .entry_content #author-name'); // get list of spans containg names
var li_to_modifiy = $('.entry_meta > li:nth-child(3)') // gets list of li elements that I need to add each name respectively
all_author_names.find('span').each(function(a,b){
console.log(b.text());
});
li_to_modifiy.find('li').each(function(a,b){
console.log(b.text());
});
您提出的更新问题的最新答案:
$('.postcontent').each(function(a,b){
$(b).find('li:eq(1)').html('<span>'+$.trim($(b).find('#author-name').text())+'</span>');
console.log($.trim($(b).text()));
});
请参阅JSFiddle