尝试jQuery wrapAll似乎将内容包装两次 [英] Trying jQuery wrapAll seems to wrap the content twice

查看:94
本文介绍了尝试jQuery wrapAll似乎将内容包装两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了这样的标记:

<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>

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

I want it to end up that way :

<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选择中丢弃它们。

So I can populate #isolated-component and easily discard them from a jQuery selection.

我试过:

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>


推荐答案

好的,我找到了原因。当您将正文内容移动到DIV时,它会移动脚本标记,包括JS移动内容并引导脚本再次执行。现在我不知道为什么这不会以无限循环结束,但是最好知道:不要移动脚本标签,你会让它们重新加载。

Ok, I found why. When you move the body content to the DIV, it moves the script tags including the JS moving the stuff and lead the script to execute a second time. Now I don't know why this doesn't end up in a infinite loop, but it's good to know : don't ever move script tags, you will make them reload.

这篇关于尝试jQuery wrapAll似乎将内容包装两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆