使用Jquery.Load()时BODY标记消失

时间:2022-11-27 21:15:39

Im trying to make a pop-up like window using jquery and its modal box. First I load the content from a html file:

我试图使用jquery及其模态框创建一个像窗口一样的弹出窗口。首先,我从html文件加载内容:

$("#test").load("test.htm");

Then I load the popup:

然后我加载弹出窗口:

$("#test").dialog("open");

This works like it should, the content of test.html is injectet into the modal pop-up. There is only one think that is wrong, and that is the BODY tags are gone from the source of the pop-up. I need the BODY tag to be there because I do some formatting based on the BODY tag.

这样就可以了,test.html的内容是注入模态弹出窗口。只有一个认为是错误的,那就是BODY标签已经从弹出窗口中消失了。我需要BODY标签,因为我根据BODY标签进行了一些格式化。

Does anyone know why jQuery.Load() removes the BODY tag? And are there any workarounds?

有谁知道为什么jQuery.Load()删除了BODY标签?有没有解决方法?

5 个解决方案

#1


0  

You are loading the HTML into an existing document that already has a body tag. A document can only have one so it automatically filters anything and extracts only the HTML inside the body tag when using load. You should wrap your HTML in a div with a specific class and do your formatting based on that class.

您正在将HTML加载到已具有正文标记的现有文档中。一个文档只能有一个,因此它在使用加载时会自动过滤任何内容并仅提取body标签内的HTML。您应该将HTML包装在具有特定类的div中,并根据该类进行格式化。

From the load docs (emphasis mine):

从加载文档(强调我的):

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". Default selector "body>*" always applies. If the URL contains a space it should be escape()d. See the examples for more information.

在jQuery 1.2中,您现在可以在URL中指定jQuery选择器。这样做会过滤传入的HTML文档,只注入与选择器匹配的元素。语法类似于“url #some> selector”。默认选择器“body> *”始终适用。如果URL包含空格,则应为escape()d。有关更多信息,请参阅示例。

#2


1  

A page can only have one body tag. If you already have one on the page, the second will be ignored.

页面只能有一个body标签。如果页面上已有一个,则忽略第二个。

In your case, it sounds like the browser is ignoring the duplicate body (nothing specific to jquery). Rather than use the body for styling, use a containing <div> with an id or class which will be retained.

在你的情况下,它听起来像浏览器忽略了重复的主体(没有特定于jquery)。不要使用正文进行样式化,而是使用包含

的id或类来保留。

#3


1  

It probably removes the body tag because it's not allowed! Each document can only have one body. Rather than force everyone to redo all their HTML pages, jQuery probably just grabs the contents of the body to use when you call load().

它可能会删除身体标签,因为它是不允许的!每个文档只能有一个正文。而不是强迫每个人重做所有的HTML页面,jQuery可能只是抓住在调用load()时要使用的主体内容。

Have you thought about perhaps wrapping everything in a containing element? eg: <div class="body"> You can then apply the exact same styles to that element.

您是否考虑过将所有内容包装在包含元素中?例如:

然后,您可以将完全相同的样式应用于该元素。

/* change this: */
body { color: #f0f; etc }

/* to this: */
body, div.body { color: #f0f; }

#4


0  

You might dynamically create the body tag using document.write of js as an alternative.

您可以使用js的document.write作为替代方法动态创建body标记。

#5


0  

I had the same issue, and solved it more or less as the following: instead of using load(), you can use get(), and do some smart string replacement:

我有同样的问题,并且或多或少解决了以下问题:不使用load(),你可以使用get(),并做一些智能字符串替换:

var content = get("test.htm")
               .replace("<body>", "<body><div class='body'>")
               .replace("</body>", "</body>");
$("#test").replace($(content).filter(".body"));

#1


0  

You are loading the HTML into an existing document that already has a body tag. A document can only have one so it automatically filters anything and extracts only the HTML inside the body tag when using load. You should wrap your HTML in a div with a specific class and do your formatting based on that class.

您正在将HTML加载到已具有正文标记的现有文档中。一个文档只能有一个,因此它在使用加载时会自动过滤任何内容并仅提取body标签内的HTML。您应该将HTML包装在具有特定类的div中,并根据该类进行格式化。

From the load docs (emphasis mine):

从加载文档(强调我的):

In jQuery 1.2 you can now specify a jQuery selector in the URL. Doing so will filter the incoming HTML document, only injecting the elements that match the selector. The syntax looks something like "url #some > selector". Default selector "body>*" always applies. If the URL contains a space it should be escape()d. See the examples for more information.

在jQuery 1.2中,您现在可以在URL中指定jQuery选择器。这样做会过滤传入的HTML文档,只注入与选择器匹配的元素。语法类似于“url #some> selector”。默认选择器“body> *”始终适用。如果URL包含空格,则应为escape()d。有关更多信息,请参阅示例。

#2


1  

A page can only have one body tag. If you already have one on the page, the second will be ignored.

页面只能有一个body标签。如果页面上已有一个,则忽略第二个。

In your case, it sounds like the browser is ignoring the duplicate body (nothing specific to jquery). Rather than use the body for styling, use a containing <div> with an id or class which will be retained.

在你的情况下,它听起来像浏览器忽略了重复的主体(没有特定于jquery)。不要使用正文进行样式化,而是使用包含

的id或类来保留。

#3


1  

It probably removes the body tag because it's not allowed! Each document can only have one body. Rather than force everyone to redo all their HTML pages, jQuery probably just grabs the contents of the body to use when you call load().

它可能会删除身体标签,因为它是不允许的!每个文档只能有一个正文。而不是强迫每个人重做所有的HTML页面,jQuery可能只是抓住在调用load()时要使用的主体内容。

Have you thought about perhaps wrapping everything in a containing element? eg: <div class="body"> You can then apply the exact same styles to that element.

您是否考虑过将所有内容包装在包含元素中?例如:

然后,您可以将完全相同的样式应用于该元素。

/* change this: */
body { color: #f0f; etc }

/* to this: */
body, div.body { color: #f0f; }

#4


0  

You might dynamically create the body tag using document.write of js as an alternative.

您可以使用js的document.write作为替代方法动态创建body标记。

#5


0  

I had the same issue, and solved it more or less as the following: instead of using load(), you can use get(), and do some smart string replacement:

我有同样的问题,并且或多或少解决了以下问题:不使用load(),你可以使用get(),并做一些智能字符串替换:

var content = get("test.htm")
               .replace("<body>", "<body><div class='body'>")
               .replace("</body>", "</body>");
$("#test").replace($(content).filter(".body"));