本文共 3007 字,大约阅读时间需要 10 分钟。
一。样式编程
1.页面中的每一个元素都具有一个style对象,此对象管理元素的CSS样式。这是在IE4.0引入的,后来作为DOM标准被接受。使用方法:
var oDiv=document.getElementById("div1");
alert(oDiv.style.backgroundColor);
style对象拥有一个cssText属性,返回描述元素样式的CSS字符串。
2.样式对象style的方法(IE6并不支持这些方法):
(1)getPropertyValue(propertyName)——返回CSS特性propertName的字符串值,比如this.style.getPropertyValue("background-color");这里的propertyName必须按照CSS的样式定义。
(2)getPropertyPriority()——返回important字符串或者为空
(3)item(index)——返回给定索引处的CSS特性名称
(4)removeProperty(propertyName)——移除某CSS特性
(5)setProperty(propertyName,value,priorty)——按照执行优先级设定CSS特性的值
3.通过隐藏层实现自定义鼠标提示的例子:
< html >
< head >
< title > Style Example </ title >
< script type ="text/javascript" src ="detect.js" ></ script >
< script type ="text/javascript" src ="eventutil.js" ></ script >
< script type ="text/javascript" >
function showTip() {
var oDiv = document.getElementById( " divTip1 " );
oDiv.style.visibility = " visible " ;
var oEvent = EventUtil.getEvent();
oDiv.style.left = oEvent.clientX + 5 ;
oDiv.style.top = oEvent.clientY + 5 ;
}
function hideTip() {
var oDiv = document.getElementById( " divTip1 " );
var oEvent = EventUtil.getEvent();
oDiv.style.visibility = " hidden " ;
}
</ script >
</ head >
< body >
< p > Move your mouse over the red square. </ p >
< div id ="div1"
style ="background-color: red; height: 50px; width: 50px"
onmouseover ="showTip()" onmouseout ="hideTip()" ></ div >
< div id ="divTip1"
style ="background-color: yellow; position: absolute; visibility: hidden; padding: 5px" >
< span style ="font-weight: bold" > Custom Tooltip </ span >< br />
More details can go here.
</ div >
</ body >
</ html >
这里使用了我在《 》中摘记的JS文件。实现可折叠区域的道理与此相同,很常用的功能,不再细说。 4.访问样式表,因为style对象只能访问一个元素的CSS样式,而对于<style/>定义的或者外部CSS文件定义的CSS规则没办法访问,这就引出了如何访问样式表的问题。我们可以通过下面的方式访问: var cssRules = document.styleSheets[ 0 ].cssRules || document.styleSheets[ 0 ].rules; document.styleSheets返回页面上所有样式表的引用,标准DOM中规定每个样式表的规则集合叫cssRules,而IE中叫rules,所以我们用上面的表达式来处理浏览器兼容问题。一个访问的例子: < html >
< head >
< title > Accessing Style Sheets Example </ title >
< style type ="text/css" >
div.special {
background-color : red ;
height : 10px ;
width : 10px ;
margin : 10px ;
}
</ style >
< script type ="text/javascript" >
function changeBackgroundColor() {
var oCSSRules = document.styleSheets[ 0 ].cssRules || document.styleSheets[ 0 ].rules;
oCSSRules[ 0 ].style.backgroundColor = " blue " ;
}
</ script >
</ head >
< body >
< div id ="div1" class ="special" ></ div >
< div id ="div2" class ="special" ></ div >
< div id ="div3" class ="special" ></ div >
< input type ="button" value ="Change Background Color" onclick ="changeBackgroundColor()" />
</ body >
</ html >
点击按钮时将三个div的背景色都设置为蓝色。 5.最终样式:顾名思义,就是样式规则(外部定义和内联的)综合计算之后呈现给用户的样式信息,用来告诉我们元素最终是如何显示在屏幕上的。IE和标准的DOM也有区别: (1)IE中的最终样式称为currentStyle对象,与style对象不同的是它是只读的,你只能读而不能给它赋值。 (2)DOM中的最终样式,通过document.defaultView.getComputedStyle(元素对象,伪元素对象或者null)方法获得 二。innerHTML和innerText 很常用的功能,特别是在AJAX应用中,经常采用的手段是从服务端返回一小段HTML代码,通过innerHTML嵌入某个层当中来动态显示下拉框等。一个比较常见的用法: oDiv.innertText=oDiv.innerText; 表示从指定元素中删除所有的HTML标签。 转载地址:http://xdktb.baihongyu.com/