如何改变css href =""用javascript? [英] How to change css href="" using javascript?

查看:89
本文介绍了如何改变css href =""用javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用js更改css href。

I want to change css href using js.

<!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>

我必须修复上面的代码。

I have to fix above code.

<link rel="stylesheet" href="u1.css" type="text/css" />

更改为

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

我可以在head标签中更改css href吗?
有可能吗?

Can I change css href in head tag? Is it possible?

推荐答案

查询它就像使用文件的任何其他元素一样.querySelector document.querySelectorAll

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

或者,您也可以通过 document.styleSheets 以及。

Alternatively, you could also access it via document.styleSheets as well.

例如:

// Change [href] on first stylesheet to u2.css
document.styleSheets[0].href = "u2.css";

这篇关于如何改变css href =&quot;&quot;用javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆