js 如何在head标签里写css代码,js如何在style标签里写入css代码?

html-css012

js 如何在head标签里写css代码,js如何在style标签里写入css代码?,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:$('head').append('<style>div{font-size: 32px}</style>')。

3、浏览器运行index.html页面,此时发现js成功将css代码添加到<head>标签的<style>标签中。

var domObj = document.getElementById("tagId")

//使用domObj.style来设置css:

domObj.style.backgroundColor="#000"//对应style里 background-color

domObj.style.fontSize="#000"//对应style里 font-size

//如果对这个表不太清楚可以在w3c上查一下

//但是一般有个规律就是,首单词小写 “-”后面的第一个字母大写,如:font-size 就是fontSize

如果是想更换标签的class的话,可以使用

domObj.className = "other_class"