javascript - 如何使用 javascript 更改 css href =""?

标签 javascript css

<分区>

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

相关文章:

javascript - 如何在网页加载时初始化图库?

javascript - 如何在严格模式下找到匿名函数的元数?

javascript - Angular 单元测试 : How to unit test . map()?

css - npm 安装后如何引用 Google Material-Design-Icons?

javascript - 使用 Jquery 打开页面时,有没有办法对某些东西产生 css 效果

php - 移动站点后WordPress更改上传路径

javascript - 在 Angular Directive(指令)中使用 require 更新 $watch

javascript - 从数据库查询 rails 创建 JSON

css - 使用 BEM 时的媒体查询

html - 如何在 Bootstrap 中创建响应式导航栏