This question already has answers here:
change the href of a css link via jquery

(3个答案)


7年前关闭。




我想使用js更改css href。
<!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" lang="ko" xml:lang="ko">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title> New Document </title>
    <link rel="stylesheet" href="u1.css" type="text/css" />
</head>

我必须修复上面的代码。
<link rel="stylesheet" href="u1.css" type="text/css" />

变成
<link rel="stylesheet" href="u2.css" type="text/css" />

我可以在head标签中更改css href吗?
是否可以?

最佳答案

像使用document.querySelectordocument.querySelectorAll的任何其他元素一样进行查询。

document.querySelector("link[href='u1.css']").href = "u2.css";

另外,您也可以通过document.styleSheets访问它。

举个例子:
// Change [href] on first stylesheet to u2.css
document.styleSheets[0].href = "u2.css";

关于javascript - 如何使用javascript更改css href =""?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22445760/

10-12 00:10
查看更多