我正在使用jquery加载一个新的html文件,但css不起作用 [英] I am using jquery load a new html file, but css doesn't work
本文介绍了我正在使用jquery加载一个新的html文件,但css不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<html>
<head>
<link rel="stylesheet" type="text/css" href="./css/public.css">
</head>
<body>
<div id="ajaxcont">
<!-- i will load some.html here by ajax -->
</div>
<script>
$("#ajaxcont").load("some.html");
</script>
</body>
</html>
some.html
看起来像这样:
<link rel="stylesheet" type="text/css" href="./css/another.css">
<div>some contents here</div>
但我发现 another.css
有时不工作,和控制台告诉我说:
But I found that another.css
sometimes doesn't work, and the console told me that:
在主线程同步的XMLHttpRequest是因为其所带来的影响最终用户的体验弃用。如需更多帮助,请查看 http://xhr.spec.whatwg.org/ 。
我想为不同的html页面加载不同的css文件,所以我不想将这些css文件放入<头>< HEAD>
提前;
And I want to load different css file for different html page, So I don't want to put those css files into the <head><head>
in advance;
推荐答案
您需要加载jQuery库,使jquery work。
You need to load the jquery library to make jquery work.
<html>
<head>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<link rel="stylesheet" type="text/css" href="../css/public.css">
</head>
<body>
<div id="ajaxcont">
<!-- i will load some.html here by ajax -->
</div>
<script>
$("#ajaxcont").load("some.html");
</script>
</body>
</html>
另外,用相对路径向上移动目录的正确方法是两个句点,而不是一个:
Also, the proper way to move up a directory with a relative path is two periods, not one:
<link rel="stylesheet" type="text/css" href="../css/public.css">
这篇关于我正在使用jquery加载一个新的html文件,但css不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文