本教程操作环境:windows7系统、javascript1.8.5版、dell g3电脑。
1.用js修改标签的 class 属性值:
class 属性是在标签上引用样式表的方法之一,它的值是一个样式表的选择符,如果改变了 class 属性的值,标签所引用的样式表也就更换了,所以这属于第一种修改方法。
更改一个标签的 class 属性的代码是:
document.getelementbyid(id值).classname = 字符串;
document.getelementbyid( id ) 用于获取标签对应的 dom 对象,你也可以用其它方法获取。classname 是 dom 对象的一个属性,它对应于标签的 class 属性。字符串 是 class 属性的新值,它应该是一个已定义的css选择符。
利用这种办法可以把标签的css样式表替换成另外一个,也可以让一个没有应用css样式的标签应用指定的样式。
举例:
<style type="text/css">.txt {font-size: 30px; font-weight: bold; color: red;}</style><div id="tt">欢迎光临!</div><p><button onclick="setclass()">更改样式</button></p><script type="text/javascript">function setclass(){document.getelementbyid( "tt" ).classname = "txt";}</script>
2.用js修改标签的 style 属性值:
style 属性也是在标签上引用样式表的方法之一,它的值是一个css样式表。dom 对象也有 style 属性,不过这个属性本身也是一个对象,style 对象的属性和 css 属性是一一对应的,当改变了 style 对象的属性时,对应标签的 css 属性值也就改变了,所以这属于第二种修改方法。
更改一个标签的 css 属性的代码是:
document.getelementbyid( id ).style.属性名 = 值;
document.getelementbyid( id ) 用于获取标签对应的 dom 对象,你也可以用其它方法获取。style 是 dom 对象的一个属性,它本身也是一个对象。属性名 是 style 对象的属性名,它和某个css属性是相对应的。
说明:这种方法修改的单一的一个css属性,它不影响标签上其它css属性值。
举例:
<div id="t2">欢迎光临!</div><p><button onclick="setsize()">大小</button><button onclick="setcolor()">颜色</button><button onclick="setbgcolor()">背景</button><button onclick="setbd()">边框</button></p><script type="text/javascript">function setsize(){document.getelementbyid( "t2" ).style.fontsize = "30px";}function setcolor(){document.getelementbyid( "t2" ).style.color = "red";}function setbgcolor(){document.getelementbyid( "t2" ).style.backgroundcolor = "blue";}function setbd(){document.getelementbyid( "t2" ).style.border = "3px solid #fa8072";}</script>
方法:
document.getelementbyid(xx).style.xxx中的所有属性是什么
盒子标签和属性对照
css语法(不区分大小写) javascript语法(区分大小写)
border border
border-bottom borderbottom
border-bottom-color borderbottomcolor
border-bottom-style borderbottomstyle
border-bottom-width borderbottomwidth
border-color bordercolor
border-left borderleft
border-left-color borderleftcolor
border-left-style borderleftstyle
border-left-width borderleftwidth
border-right borderright
border-right-color borderrightcolor
border-right-style borderrightstyle
border-right-width borderrightwidth
border-style borderstyle
border-top bordertop
border-top-color bordertopcolor
border-top-style bordertopstyle
border-top-width bordertopwidth
border-width borderwidth
clear clear
float floatstyle
margin margin
margin-bottom marginbottom
margin-left marginleft
margin-right marginright
margin-top margintop
padding padding
padding-bottom paddingbottom
padding-left paddingleft
padding-right paddingright
padding-top paddingtop
颜色和背景标签和属性对照
css 语法(不区分大小写) javascript 语法(区分大小写)
background background
background-attachment backgroundattachment
background-color backgroundcolor
background-image backgroundimage
background-position backgroundposition
background-repeat backgroundrepeat
color color
样式标签和属性对照
css语法(不区分大小写) javascript 语法(区分大小写)
display display
list-style-type liststyletype
list-style-image liststyleimage
list-style-position liststyleposition
list-style liststyle
white-space whitespace
文字样式标签和属性对照
css 语法(不区分大小写) javascript 语法(区分大小写)
font font
font-family fontfamily
font-size fontsize
font-style fontstyle
font-variant fontvariant
font-weight fontweight
文本标签和属性对照
css 语法(不区分大小写) javascript 语法(区分大小写)
letter-spacing letterspacing
line-break linebreak
line-height lineheight
text-align textalign
text-decoration textdecoration
text-indent textindent
text-justify textjustify
text-transform texttransform
vertical-align verticalalign
【推荐学习:javascript高级教程】
以上就是js怎么改变css属性值的详细内容。
