Php jQuery使用数组包装并放置一个类
我想将h3标记和div包装到一个带有follow数组类的div中,我如何使用jQuery实现这一点 这是我当前的代码:Php jQuery使用数组包装并放置一个类,php,jquery,arrays,Php,Jquery,Arrays,我想将h3标记和div包装到一个带有follow数组类的div中,我如何使用jQuery实现这一点 这是我当前的代码: $sections = array('section1','section2','section3'); <h3>Section Title 1</h3> <div class="options_1"> <p>Some Options</p> </div> <h3>Section
$sections = array('section1','section2','section3');
<h3>Section Title 1</h3>
<div class="options_1">
<p>Some Options</p>
</div>
<h3>Section Title 2</h3>
<div class="options_2">
<p>Some Options</p>
</div>
<h3>Section Title 3</h3>
<div class="options_3">
<p>Some Options</p>
</div>
$sections=array('section1'、'section2'、'section3');
第1节标题
一些选择
第2节标题
一些选择
第3节标题
一些选择
这就是我想要实现的目标
$sections = array('section1','section2','section3');
<div class="section1">
<h3>Section Title 1</h3>
<div class="options_1">
<p>Some Options</p>
</div>
</div>
<div class="section2">
<h3>Section Title 2</h3>
<div class="options_2">
<p>Some Options</p>
</div>
</div>
<div class="section3">
<h3>Section Title 3</h3>
<div class="options_3">
<p>Some Options</p>
</div>
</div>
$sections=array('section1'、'section2'、'section3');
第1节标题
一些选择
第2节标题
一些选择
第3节标题
一些选择
非常感谢您的帮助。
谢谢假设您希望新div的类名是
标记的文本,您可以这样做:
$("h3").each(function() {
var text = $(this).text();
var section = $('<div class="' + text + '"></div>');
section.insertBefore(this);
var next = $(this).next();
section.append(this).append(next);
});
$(“h3”)。每个(函数(){
var text=$(this.text();
var部分=$('');
第节。在(本)之前插入;
var next=$(this.next();
第.append节(此).append节(下一个);
});
这将获取每个
的文本,创建一个新的div,并将其作为类名,插入
标记之前,然后插入
和以下同级对象作为新div的子对象。var$sections=['section1','section2','section3'];
var $sections = ['section1', 'section2', 'section3'];
$("h3").each(function(idx){
var next = $(this).next();
var c = $sections[idx];
$(this).wrap('<div class="'+c+'" />').parent().append(next);
});
$(“h3”)。每个(函数(idx){
var next=$(this.next();
var c=$sections[idx];
$(this.wrap(“”).parent().append(next);
});
这不包括h3标记下的div。这一个可以工作,但我想将类名与我指定的数组一起使用。假设你有第1部分的标题,那么在课堂上使用h3的文本是行不通的;