如何在文档中的所有.firstlevel p标记周围包装div标记。基本上为所有第一级p节点创建父节点,并为所有目标类重复相同的过程:.secondlevel和.thirdlevel p nodes。
我在论坛中根据类似问题尝试了一些方法但没有成功。
当前代码生成的文档不是所需的格式。 使用当前div围绕连续的p.firstlevel节点而不是围绕所有p.firstlevel节点,但我需要div将所有p.firstlevel从文档的开头包装到结尾。 p.secondlevel,p.thirdlevel都被正确包裹。
我的问题是如何将div从第一个p.firstlevel开始包装到最后一个p.firstlevel,如所需的输出所示?
以下是我从here改编的代码:
$(".firstlevel")
.filter(function() {
return !$(this).prev().is(".secondlevel");
})
.map(function() {
return $(this).nextUntil(".firstlevel").andSelf();
}).wrap('<div class="wrap" />');
示例源html
<p class="generic">Lorem ipsum dolore</p>
<p class="firstlevel">One - Quibusdam, asperiores
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="secondlevel">Two - Lorem ipsum dolore
<p class="thirdlevel">Three - Pariatur, consequuntur.</p>
<p class="thirdlevel">Three - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="secondlevel">Two - Lorem ipsum dolore
<p class="thirdlevel">Three - Lorem ipsum dolore
<p class="thirdlevel">Three - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="secondlevel">Two - Lorem ipsum dolore
<p class="secondlevel">Two - Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Dolor repellendus</p>
<p class="class_gen">Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore</p>
期望的输出
<p class="generic">Lorem ipsum dolore</p>
<div class="first">
<p class="firstlevel">One - Quibusdam, asperiores
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<div class="second">
<p class="secondlevel">Two - Lorem ipsum dolore
<div class="third">
<p class="thirdlevel">Three - Pariatur, consequuntur.</p>
<p class="thirdlevel">Three - Lorem ipsum dolore
</div>
<p class="class_gen">Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
</div>
<div class="second">
<p class="secondlevel">Two - Lorem ipsum dolore
<div class="third">
<p class="thirdlevel">Three - Lorem ipsum dolore
<p class="thirdlevel">Three - Lorem ipsum dolore
</div>
<p class="class_gen">Lorem ipsum dolore
</div>
<p class="firstlevel">One - Lorem ipsum dolore
<div class="second">
<p class="secondlevel">Two - Lorem ipsum dolore
<p class="secondlevel">Two - Lorem ipsum dolore
</div>
<p class="firstlevel">One - Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore
<p class="firstlevel">One - Dolor repellendus </p>
</div>
<p class="class_gen">Lorem ipsum dolore
<p class="class_gen">Lorem ipsum dolore</p>
使用(.firstlevel,.secondlevel,.thirdlevel等)类包装目标p节点时,文档结构不应更改。
答案 0 :(得分:0)
对于任何可能需要相同解决方案或方法的人来说,这是我的问题的答案。我改编了上面提到的解决方案。
var jsdom = require("jsdom");
var fs = require("fs");
var jquery = fs.readFileSync("./js/jquery.js", "utf-8");
jsdom.env({
url: "http://127.0.0.1:8000",
src: [jquery],
done: function(errors, window) {
var $ = window.$;
$(".firstlevel").first().removeClass("firstlevel").addClass("firstone");
$(".firstlevel").last().removeClass("firstlevel").addClass("lastone");
//Working as expected
$(".firstone").map(function() {
if (!$(this).prev().hasClass("firstlevel")) {
return $(this).prev().nextUntil(".lastone").next().addBack();
}
}).wrap("<div class='wrap' />");
//Working as expected
$(".secondlevel").map(function() {
{
return $(this).nextAll(".thirdlevel").addBack();
}
}).wrap("<div class='wrap' />");
//Working as expected
$(".thirdlevel").map(function() {
if (!$(this).prev().hasClass("thirdlevel")) {
return $(this).nextUntil(":not(.thirdlevel)").addBack();
}
}).wrap("<div class='wrap' />");
console.log($('body').html());
}
});
这是JSFiddle上的working demo