尝试 jQuery wrapAll 似乎将内容包装两次

2024-01-14

我得到了一个如下所示的标记:

<body>
    <...
    a lot of tags from the web site
    ...>
    <script type="text/javascript" src="http://cdn.jquerytools.org/1.1.1/jquery.tools.min.js"></script>
    <script type="text/javascript" src="my_script.js"></script>
<body>

我希望它以这种方式结束:

<body>
    <div id="body-content">
        <...
        a lot of tags from the web site
        ...>
        <script type="text/javascript" src="http://cdn.jquerytools.org/1.1.1/jquery.tools.min.js"></script>
        <script type="text/javascript" src="my_script.js"></script>
    </div>
    <div id="isolated-component">
</div>
<body>

因此我可以填充 #isolated-component 并轻松地从 jQuery 选择中丢弃它们。

我尝试过:

jQuery("body").children().wrapAll("<div id='body-content'></div>")
                        .after("<div id='isolated-component'></div>");

但最终得到:

<html>
<head>
<body>
    <div id="body-content">
        <div id="body-content">
            <... page content ...>
        </div>
        <div id="isolated-component"/>
        <div id="isolated-component"/>
    </div>
    <script src="my_script.js" type="text/javascript">
    </script>
</body>
<div id="_firebugConsole" style="display: none;" FirebugVersion="1.4.3" methodName="log"/>
</html>

好吧,我找到原因了。当你将body内容移动到DIV时,它会移动脚本标签,包括移动内容的JS,并导致脚本第二次执行。现在我不知道为什么这不会以无限循环结束,但很高兴知道:永远不要移动脚本标签,你会让它们重新加载。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试 jQuery wrapAll 似乎将内容包装两次 的相关文章

随机推荐