jquery - 在jQuery中使用addClass调用另一个文件中写的CSS类

标签 jquery css function external addclass

我想从 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/

相关文章:

jquery - 当视口(viewport)宽度小于 HTML 页面宽度时,如何让固定元素水平滚动?

javascript - 如何使用 jquery 或 javascript 知道当前在元素上运行哪个动画

c++ - 当其标识符已经是结构的成员时如何使用枚举器列表项?

javascript - 访问 QuillJS 中的书面文本

html - 使用 HTML 和 CSS 进行页面布局

html - 将鼠标悬停在 li 上使第二行 li 移动

javascript - 类型错误 : d is null

function - 动态确定 Go 中的函数参数

java - 如何制作一个减少数字的函数(例如从 800 毫秒减少到 200 毫秒)

javascript - 将鼠标悬停在其他元素下方