使用jQuery将内容包装在div中 [英] wrap content in a div using jquery
问题描述
我正在尝试将内容包装在div中,但是问题是html页面不可编辑,所以我正在尝试其他方法,使用jQuery将所有内容包装在 div
中,以下是 html
结构
I am trying to wrap content in a div but the problem is the html page is not editable so I am trying other way, using jQuery to wrap all the content in a div
following is the html
structure
$(document).ready(function() {
$("hr").before("<div class=wrapElement>");
$("#disqus_thread").before("</div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<h4>Title Here</h4>
<p> </p>
<p> </p>
<p> </p>
<div id="disqus_thread"></div>
问题,打开 div
的位置正确,但是关闭div的位置不正确,应该位于 div id ="disqus_thread" 代码>,但不存在,如何将其放置在此位置?
Problem opening div
is placed on correct position, but closing div is not on correct position, it should be above div id="disqus_thread"
but its not there, how do I get it placed on this position?
jQuery版本是1.12.4
jQuery version is 1.12.4
预先感谢
推荐答案
使用 nextUntil()
方法将所有元素保留到div并使用 wrapAll()
方法,以使用 div
元素包装它们.
Use nextUntil()
method to get all elements upto the div and use wrapAll()
method to wrap them using a div
element.
$(document).ready(function() {
$("hr").nextUntil("#disqus_thread") // get elements from hr upto the previous element of #disqus_thread
.wrapAll("<div class=wrapElement></div>"); // wrap all elements using div
});
$(document).ready(function() {
$("hr").nextUntil("#disqus_thread").wrapAll("<div class=wrapElement></div>");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<hr>
<h4>Title Here</h4>
<p> </p>
<p> </p>
<p> </p>
<div id="disqus_thread"></div>
这篇关于使用jQuery将内容包装在div中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!