Ajax请求有效,但没有CSS

时间:2022-10-07 14:56:15

Below is a snippet of code I have for an Ajax request. The request works, but when the request is processed the page appears without any of the CSS (even though I have everything in the same directory). To test this I made the request point to a page on my site that already existed. Any help? Thanks in advance.

下面是我为Ajax请求提供的一段代码。该请求有效,但是当处理请求时,页面出现时没有任何CSS(即使我在同一目录中有所有内容)。为了测试这个,我将请求指向我网站上已经存在的页面。有帮助吗?提前致谢。

<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","ajaxtest.html",true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>

4 个解决方案

#1


1  

Injecting css in a page via Ajax is not supported by all browsers (whether inline or via a <link> tag).

所有浏览器都不支持通过Ajax在页面中注入css(无论是内联还是通过 标记)。

The solution is to load the CSS for the ajax content in the page containing the Ajax call.

解决方案是在包含Ajax调用的页面中加载ajax内容的CSS。

#2


0  

According to W3 Schools, a <link> element for a stylesheet can only appear in your document <head>.

根据W3 ​​Schools,样式表的 元素只能出现在您的文档中。

Hence if you include the contents of a whole external file (that links its own styles) within your <div> those won't be loaded.

因此,如果在

中包含整个外部文件(链接自己的样式)的内容,则不会加载这些内容。

#3


0  

The problem is that you are overwriting the H2 tag with the innerHTML. if you had something like

问题是你用innerHTML覆盖了H2标签。如果你有类似的东西

<h2 id="myDiv">Let AJAX change this text</h2>

It would keep the h2. Or you need to have the AJAX pass back:

它会保持h2。或者你需要传回AJAX:

<h2>What I want to change it to</h2>

#4


0  

Move your css inside a <style> element inside the pege you request with ajax. Should do the trick.

使用ajax将您的CSS移动到您请求的pege内的

Second way is to insert the css for the page you request with ajax into the css file of the page from witch you request.

第二种方法是将您使用ajax请求的页面的css插入到您请求的页面的css文件中。

#1


1  

Injecting css in a page via Ajax is not supported by all browsers (whether inline or via a <link> tag).

所有浏览器都不支持通过Ajax在页面中注入css(无论是内联还是通过 标记)。

The solution is to load the CSS for the ajax content in the page containing the Ajax call.

解决方案是在包含Ajax调用的页面中加载ajax内容的CSS。

#2


0  

According to W3 Schools, a <link> element for a stylesheet can only appear in your document <head>.

根据W3 ​​Schools,样式表的 元素只能出现在您的文档中。

Hence if you include the contents of a whole external file (that links its own styles) within your <div> those won't be loaded.

因此,如果在

中包含整个外部文件(链接自己的样式)的内容,则不会加载这些内容。

#3


0  

The problem is that you are overwriting the H2 tag with the innerHTML. if you had something like

问题是你用innerHTML覆盖了H2标签。如果你有类似的东西

<h2 id="myDiv">Let AJAX change this text</h2>

It would keep the h2. Or you need to have the AJAX pass back:

它会保持h2。或者你需要传回AJAX:

<h2>What I want to change it to</h2>

#4


0  

Move your css inside a <style> element inside the pege you request with ajax. Should do the trick.

使用ajax将您的CSS移动到您请求的pege内的

Second way is to insert the css for the page you request with ajax into the css file of the page from witch you request.

第二种方法是将您使用ajax请求的页面的css插入到您请求的页面的css文件中。