样式属性即操作在DOM2级中的变化
2017-03-08 15:36
337 查看
在DOM2级中规定float样式属性的访问方式可以是:cssFloat(chrom、Firefox)、styleFloat,但是也可以直接使用float来访问float样式属性
2.style对象在DOM2级中的变化
新增了属性和方法1.cssText: 某个元素所有样式值拼接的字符串
2.length:返回样式个数
3.item:获取指定位置上的样式
4.getPropertyValue(属性名):获取对应样式属性名的样式属性值
5.removeProperty(属性名):删除样式
6.setProperty(属性名,属性值,优先级):设置属性
3.获取计算后的样式
1.在chrom,firefox中:document.defaultView.getComputedStyle.getPropertyValue(属性名)2.在IE中:document.currenStyle.属性名
4.操作样式表
1.styleSheets对象:这是一个类数组对象,保存了所有link和style方式引入的样式表,其中保存的对象是CSSStyleSheet,如:document.styleSheets2.CSSStyleSheet:这对象表示style或link对应的样式表对象,包括的属性:
1.disabled:样式表是否可用,true不可用,false可用
2.href:link元素的href属性的值,如果是style元素那么为null
3.cssRules:是一个类数组对象,保存了所有的样式规则对象 CSSStyleRule, // IE8以下中为rules属性
CSSStyleRule这个对象包含属性和方法:
1.cssText:样式规则字符串,包含选择器 如:cssText: "#box
{ width: 200px; height: 200px; } //IE8中没有这个属性
2.style:就是CSSStyleDeclaration即我们常获取的style对象
3.selectorText:样式规则的选择器 如:#box
4.insertRule(规则字符串,位置):
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>demo</title> <style> #box{ width:200px; height: 200px; background-color: #2c82c9; } body{margin: 0px; padding: 0px;} </style> <link type="text/css" rel="stylesheet" href="edit/css/font-awesome.css"> </head> <body> <div id="box">fsfdsf</div> </body> <script> var sheet=document.getElementsByTagName("style")[0].sheet; insertRule(sheet,"#box","background-color:red",sheet.cssRules[0].style.length-1); function insertRule(sheet, selectorText, cssText, position){ if (sheet.insertRule){ //chrom 、firefox sheet.insertRule(selectorText + "{" + cssText + "}", position); } else if (sheet.addRule){ //IE sheet.addRule(selectorText, cssText, position); } } </script> </html>
5.deleteRule(样式属性位置) //chrom、firefox 或 removeRule //ie
4.ownerNode:样式表对应的元素节点对象,一般指style和link
备注:获取CSSStyleSheet对象的方式是:先获取style或link元素节点对象,然后使用sheet(chrom、firefox)或styleSheet(IE)直接获取到
相关文章推荐
- js学习小结(十)2014.5.10.11(DOM2和DOM3的变化,操作样式的DOM API(1))
- jQuery之dom操作(属性、样式操作)
- 使用DOM操作样式属性
- JavaScript学习笔记——DOM_对document对象的内容、属性、样式的操作
- Dom的样式操作和属性操作
- Dom样式操作-属性操作
- DOM_window对象属性之--clipboardData对象操作代码
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
- 使用jQuery操作元素的属性与样式
- JavaScript操作DOM元素中的class属性(转载)
- 从零开始学习jQuery(4)-使用jQuery操作元素的属性与样式
- JQuery基础之(五)使用JQuery操作元素的属性和样式
- js操作dom元素的属性和方法
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
- <table>标签 利用DOM 的方法和属性实现对表格的动态操作
- JS操作dom属性与方法
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
- DOM方法操作HTML属性
- jQuery学习整理 (3) 使用jQuery操作元素的属性与样式