网站制作学习网JS脚本→正文:Dom样式编程
字体:

Dom样式编程

JS脚本 2010/4/17 22:44:16  点击:不统计

<网f站o学a习s制p作.cn>

Dom样式编程,简单的理解就是通过javascript来操作页面元素的属性,来改变页面显示效果。页面中通过引入style对象来管理元素的css属性。Dom也采用style对象来管理。
style对象包含与每个CSS样式对应的特性,虽然格式不同,单个单词的css样式,以相同名字的特性来表示,但是style对象中,两个单词的样式的表示方式是通过将第一个单词加

上首字母大写的第二个残次,并且单词间没有横杠。
下面列出了一些常用的CSS特性以及她们对应的javascript中style对象的表示
CSS样式特性       javascript样式特性
background-color  style.backgroundColor
color             style.color
font              style.font
font-family       style.fontFanmily
font-weight       style.fontWeight
以上是举了几个简单的例子。

转www.载for网站制作学习asp必.cn究

<html>
<title>网站制作学习网</title>
<head>
<script language="javascript">
function forasp_cn()
{
var forasp_cn_div = document.getElementById("forasp_div");
alert("原来颜色是"+forasp_cn_div.style.backgroundColor+"\n点击后变为green");
forasp_cn_div.style.backgroundColor = "green";
}
</script>
</head>
<body>
<div id="forasp_div" style="background-color:red;" onclick= "forasp_cn();">网站制作学习网点击看看</div>
</body>
</html>
style对象还有cssText特性,这个特性包含了所有描述元素样式的CSS字符串。
用法:对象.style.cssText。这样就获取了全部的style的样式内容。

Dom样式的方法
getPropertyValue(properyName)-返回CSS特性propertyName的字符串值。特性必须按照CSS样式定义。例如"background-color"而不是"backgroundColor";
getPropertyPriority()-如果在规则中指定特性important,则返回important,否则返回空的字符串。
item(index)-返回在给定索引的index处的css特性的名称,称"background-color";
removeProperty(propertyName)-从CSS定义中删除propertyName
setProperty(propertyName,value,priority)-按照指定的优先级priority来设置CSS特性propertyName的value值(priority的值可以为impertinent或者为空字符串)
 


%77w%77%2E%66%6F%72%61%73%70%2E%63%6E

·上一篇:跨平台(格式化)event >>    ·下一篇:javascript之Dom访问CSS样式表类 >>
推荐文章
最新文章