我想从 JQuery 函数(也包括外部文件)调用在 CSS 文件中编写的 CSS 类。
它不起作用我想知道为什么:
jQuery :
/// <reference path="jquery-1.7.1.js" />
/// <reference path="jquery-1.7.1-vsdoc.js" />
/// <reference path="main_style.css" />
function gg() {
alert("zombie");
$("p").addClass("blood");
};
CSS:
.blood
{
color: Aqua;
background-color: Red;
border: 2;
font-weight: bold;
}
我在 jQuery 文件中添加了对 CSS 文件的引用,但我不明白为什么它找不到它并且没有更改所有段落。
但“gg”功能已达到,因为我看到了僵尸警报。
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.7.1-vsdoc.js" type="text/javascript"></script>
<link href="Scripts/main_style.css" rel="stylesheet" type="text/css" />
<script src="Scripts/mon_script.js" type="text/javascript"></script>
<title></title>
</head>
<body>
<div>
<p>This is the first paragraph</p>
<p id="4">This is the second paragraph</p>
<p>This is the third paragraph</p>
<p class="blood">This is the fourth paragraph</p>
<input type = "button" value="Hi" onclick="gg()" />
</div>
</body>
</html>
最佳答案
您需要在包含您的 jquery 脚本的 html 文档(即 ASP.NET 中的 aspx 文件)中添加样式表 main_style.css 的链接。您指定的引用路径只是为了支持 IntelliSense。
如果 css 文件和 html 文件都处于相同的相对位置,则 HTML 文件中的样式表链接将如下所示(否则使用 css 文件的相对路径):
<LINK href="main_style.css" rel="stylesheet" type="text/css">
关于jquery - 在jQuery中使用addClass调用另一个文件中写的CSS类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8462272/