我有以下结构:
<div class="cardapio-content">
<h5>Tittle of tab 1</h5>
<p>Content of tab 1</p>
<h5>Tittle of tab 2</h5>
<p>Content of tab 2</p>
<h5>Tittle of tab 3</h5>
<p>Content of tab 3</p>
</div>
并希望获取每个h5
并添加到另一个div <div class="cardapio-menu">
,将p
下方的h5
内容与每个标签相关联。
我尝试使用each
,但不知道如何将p
相对于h5
索引...
jQuery('.cardapio-content h5').each(function(k, v) {
alert(jQuery('.cardapio-content')[k].html());
})
感谢。
答案 0 :(得分:4)
只需使用.next()
:
jQuery('.cardapio-content h5').each(function(){
var h5 = $(this);
var p = $(this).next();
});
答案 1 :(得分:2)
使用jQuery的.eq
来获取你想要的元素。
jQuery('.cardapio-content h5').each(function(k, v) {
alert(jQuery('.cardapio-content p').eq(k).html());
})
答案 2 :(得分:1)
仅在each
函数中使用了.next()和.andSelf()选择器,将它们包装在一起:
$('.cardapio-content h5').each(function(){
$(this).next('p').andSelf().wrapAll('<div class="cardapio-menu" />');
});