美文网首页基础JS
JS常用API手册7-样式

JS常用API手册7-样式

作者: 桃花島主 | 来源:发表于2017-11-24 11:23 被阅读12次

oDiv.style //返回的是一个对象
返回的是设置的style样式
作用:用来设置值

# 例如
style="width:100px;height:100px;border-color:#fff"
console.log(oDiv.style.width);          //100px
console.log(oDiv.style.borderColor)     //#fff
oDiv.style.borderLeftColor;
oDiv.style.border = '1px solid red';
oDiv.style.backgroundColor;             //颜色值FF返回的始终是RGB
oDiv.style.cssText;                     //返回浏览器设置的style[结果是浏览器渲染后的结果]
Div.style.cssText="width:20px;height:10px;display:bolck";       //把多个样式进行合并提高了浏览器渲染的效率

实际的样式

# IE
oDiv.currentStyle
oDiv.currentStyle.width;
oDiv.currentStyle.height;

# W3C
getComputedStyle(oDiv, null);                                                           //null没有实现
getComputedStyle(oDiv, null).width
getComputedStyle(oDiv, null).borderLeftColor;  //复合属性要写的详细些
getComputedStyle(oDiv, null).backgroundColor;

document.styleSheets

说明:样式文件的伪数组
属性:
type:text/css
href:链接地址
cssText(IE):样式表中规则的文本形式
rules(IE),
cssRules(W3C):对应样式表里所有规则的集合[都是数组的形式]
Rule对象属性:
selectText:选择符
style,可以读取和设置CSS规则,并且由cssText属性和内联样式的方式一样

rules[ruleIndex].style.width;
rules[ruleIndex].style.width = "500px";                 //样式比较隐蔽,查看代码是看不出来的

修改样式表

# FF:insertRule(cssText, cssIndex);
document.styleSheets[0].insertRule("body{color:blue;}", 1);

# IE:addRule(selector, cssText, cssIndex);
document.selector[0].addRule("body", "color:blue", 1);

# FF:deleteRule(cssIndex)
document.styleSheets[0].deleteRule(1);

# IE:removeRule(cssIndex)
document.styleSheets[0].removeRule(1);

特殊的属性值

# float
element.style.cssFloat         //W3C
element.style.styleFloat       //IE

获取css样式

// ie8
el.currentStyle[attrName]
// ie9+
window.getComputedStyle(el)[attrName]
// 伪类
window.getComputedStyle(el , ":after")[attrName];

相关文章

  • JS常用API手册7-样式

    oDiv.style //返回的是一个对象返回的是设置的style样式作用:用来设置值 实际的样式 docume...

  • JS常用API手册7-定位

    oDiv.clientWidth, oDiv.clientHeight 兼容性:全兼容内容区域的宽度、高度,不包括...

  • JS常用API手册1

    删除[数组,对象]的属性-变量 i的y次方 去掉数组的第一个元素 dom对象 每个标签都是一个对象 todo un...

  • JavaScript网页特效(一)

    介绍常用js的字符串操作api ❤❤❤ 介绍常用js的保留小数操作api ❤❤ 介绍常用js的大小写转换操作a...

  • iOS ReactiveCocoa 最全常用API整理(可做为手

    iOS ReactiveCocoa 最全常用API整理(可做为手册查询) GitHub

  • vue axios cdn 封装

    1.config.js 常用域名封装: 2.utils.js 常用的方法封装: 3.api.js axios接口...

  • JS常用API手册3-Snippets

    判断数据类型 百度自动选择效果 清除文本选中状态 IE当鼠标移除窗口之后仍能捕获事件 IE释放捕获 浏览器检测 加...

  • JS常用API手册6-事件

    mouse 注:offset更好用一些,高级浏览器也支持layer只有对象有定位信息时才好用,比如:positio...

  • JS常用API手册5-dom

    window.history.go(-1) window.history.forward() window.his...

  • js常用API

    一、节点 节点属性 节点操作 Document节点 Element节点 对象

网友评论

    本文标题:JS常用API手册7-样式

    本文链接:https://www.haomeiwen.com/subject/eomxbxtx.html