简体   繁体   English

在第一个孩子之前和之后插入带有javascript的html标签时,避免浏览器自动关闭标签

[英]avoid browser auto close tag when inserting html tag with javascript before first-child and after

I want to add before first .free-column DIV and after last .free-column DIV But browser auto-closes section tag 我想在第一个.free-column DIV之前和之后添加.free-column DIV,但是浏览器会自动关闭部分标签

This is my code: 这是我的代码:

<div class="free-column">
    <article>
        article
    </article>
</div>
<div class="free-column">
    <article>
        article
    </article>
</div>
<article>
    column
</article>
<div class="free-column">
    <article>
        article
    </article>
</div>

<script type="text/javascript">
jQuery(function() {

    // this is OK
    jQuery("<section class='row container'>").insertBefore(".free-column:first");
    // this closing tag should appear at the bottom but browser auto-close it
    jQuery("</section>").insertAfter(".free-column:last");

    // just testing, this element appears at the end succesfully
    jQuery("<div class='test'>JS_test</div>").insertAfter(".free-column:last");

});
</script>

jQuery doesn't really let you manipulate opening/closing tags, rather the whole element at once. jQuery并没有真正让您操纵打开/关闭标签,而是一次操纵了整个元素。

You could try the wrapAll method, which wraps the selected elements inside a new element: 您可以尝试wrapAll方法,该方法将所选元素包装在新元素内:

$(".free-column").wrapAll("<section class='row container' />");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM