jQuery-围绕标题和下一个非标题的块元素创建容器
我想将标题和标题后面的第一个非标题块元素包装在一个div中,这样我就可以控制分页符并确保标题与后续文本一起打印 例1:jQuery-围绕标题和下一个非标题的块元素创建容器,jquery,Jquery,我想将标题和标题后面的第一个非标题块元素包装在一个div中,这样我就可以控制分页符并确保标题与后续文本一起打印 例1: <h1>Main title</h1> <h2>Sub title</h2> <h3>Another sub title</h3> <p>Paragraph text here...</p> <p>And more paragraph text here</p>
<h1>Main title</h1>
<h2>Sub title</h2>
<h3>Another sub title</h3>
<p>Paragraph text here...</p>
<p>And more paragraph text here</p>
主标题
副标题
另一个副标题
段落文本在这里
还有更多的段落文本
将成为
<div class="PrintTogether">
<h1>Main title</h1>
<h2>Sub title</h2>
<h3>Another sub title</h3>
<p>Paragraph text here...</p>
</div>
<p>And more paragraph text here</p>
<div class="PrintTogether">
<h2>Some title</h2>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
</div>
<p>And more paragraph text here</p>
主标题
副标题
另一个副标题
段落文本在这里
还有更多的段落文本
例2:
<h2>Some title</h2>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
<p>And more paragraph text here</p>
一些标题
项目1
项目2
项目3
还有更多的段落文本
将成为
<div class="PrintTogether">
<h1>Main title</h1>
<h2>Sub title</h2>
<h3>Another sub title</h3>
<p>Paragraph text here...</p>
</div>
<p>And more paragraph text here</p>
<div class="PrintTogether">
<h2>Some title</h2>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
</div>
<p>And more paragraph text here</p>
一些头衔
项目1
项目2
项目3
还有更多的段落文本
到目前为止,我整理的jQuery是:
$('h1, h2, h3, h4').each(function() {
// ignore if header is already wrapped in PrintTogether div
if (! $(this).parents('.PrintTogether').length ) {
$(this).nextUntil('p, ol, ul, table').andSelf().wrapAll('<div class="PrintTogether" />');
}
});
$('h1,h2,h3,h4')。每个(函数(){
//如果标题已包装在PrintTogether div中,则忽略
if(!$(this).parents('.printogether').length){
$(this).nextUntil('p,ol,ul,table')。和self().wrapAll('');
}
});
我的问题是,我无法将最后一个元素(
或上述示例中的
)包含在PrintTogether div中。这是我当前得到的:
<div class="PrintTogether">
<h1>Main title</h1>
<h2>Sub title</h1>
<h3>Another sub title</h3>
</div> <!-- should be after next p block -->
<p>Paragraph text here...</p>
<p>And more paragraph text here</p>
主标题
副标题
另一个副标题
段落文本在这里
还有更多的段落文本
谢谢你的帮助 我想你需要排除第一段 检查下面更新的代码。希望这有帮助
$('h1, h2, h3, h4').each(function() {
// ignore if header is already wrapped in PrintTogether div
if (! $(this).parents('.PrintTogether').length ) {
$(this).nextUntil('ol, ul, table,p:not(:first)').andSelf().wrapAll('<div class="PrintTogether" />');
}
});
$('h1,h2,h3,h4')。每个(函数(){
//如果标题已包装在PrintTogether div中,则忽略
if(!$(this).parents('.printogether').length){
$(this).nextUntil('ol,ul,table,p:not(:first)).andSelf().wrapAll(“”);
}
});
如果
p
始终是div.printogether
之后的第一个元素,那么您可以仅使用nextUntil('p:not(:first)
。您可以在生成的包装器之后获得下一个元素。printogether
包装器并将其移动到包装器内部
$(文档).ready(函数(){
$('h1,h2,h3,h4')。每个(函数(){
//如果标题已包装在PrintTogether div中,则忽略
if(!$(this).parents('.printogether').length){
变量标题=$(this).nextUntil('p,ol,ul,table')。和self().wrapAll('');
var wrapper=headers.eq(0.parent();
wrapper.next().appendTo(包装器);
}
});
});代码>
。一起打印{
背景色:rgba(50,50,60,2);
填充:15px
}
标题
一些头衔
一些头衔
一些头衔
项目1
项目2
项目3
此处还有更多段落文本
,感谢您的帮助。我能用你建议的方法让它工作。