zoukankan      html  css  js  c++  java
  • javascript操作html元素CSS属性

    下面先记录一下JS控制CSS所使用的方法. 
    
    
    1.使用javascript更改某个css class的属性... 
    
    
    <style type="text/css"> 
    .orig { 
    display: none; 
    } 
    </style> 
    你想要改变把他的display属性由none改为inline。 
    解决办法: 在IE里: 
    
    document.styleSheets[0].rules[0].style.display = "inline"; 
    在firefox里: 
    
    document.styleSheets[0].cssRules[0].style.display = "inline"; 
    讨论: 可以做一个函数来搜索特定名字的style对象: 
    
    关于rules和cssRules的浏览器兼容性,本人博文有测试记录:http://blog.csdn.net/u011043843/article/details/28276757
    
    [javascript] view plaincopy
    function getstyle(sname) {   
    for (var i=0;i<document.styleSheets.length;i++) {   
    var rules;   
    if (document.styleSheets[i].cssRules) {   
    rules = document.styleSheets[i].cssRules;   
    } else {   
    rules = document.styleSheets[i].rules;   
    }   
    for (var j=0;j<rules.length;j++) {   
    if (rules[j].selectorText == sname) {   
    //selectorText 属性的作用是对一个选择的地址进行替换.意思应该是获取RULES[J]的CLASSNAME.有说错的地方欢迎指正   
    return rules[j].style;   
    }   
    }   
    }   
    }   
    
    
    然后只要: 
    
    getstyle(".orig").display = "inline"; 
    就可以了。 
    
    ------------------ 注意 document.styleSheets[0].rules[0].style 这个 styleSheets[0]数组的下标是代表本页的第N个CSS样式表,它的下级rules[0]的数组下标表示的则是这个样式表中的第N个样式,例如: 
    <style type="text/css"> 
    .s{display="none";} 
    .w{display="none";} 
    </style> 
    修改S则: document.styleSheets[0].rules[0].style.display='inline'; 
    修改W则:document.styleSheets[0].rules[1].style.display = 'inline'; 
    注意:CSS和HTML结合的方式必须为<LINK rel="stylesheet" type="text/css" href="" /><style></style>的时候以上方法可行,如@IMPORT 则不行. 
    ==================================== 
    
    下面记录一下JS访问CSS中的样式: 
    用javascript获取和设置style 
    DOM标准引入了覆盖样式表的概念,当我们用document.getElementById("id").style.backgroundColor 获取样式时 获取的只是id中style属性中设置的背景色,如果id中的style属性中没有设置background-color那么就会返回空,也就是说如果id用class属性引用了一个外部样式表,在这个外部样式表中设置的背景色,那么不好意思
    document.getElementById("id").style.backgroundColor 这种写法不好使,如果要获取外部样式表中的设置,需要用到window对象的getComputedStyle()方法,代码这样写window.getComputedStyle(id,null).backgroundColor 
    但是兼容问题又来了,这么写在firefox中好使,但在IE中不好使 
    两者兼容的方式写成 
    window.getComputedStyle?window.getComputedStyle(id,null).backgroundColor:id.currentStyle["backgroundColor"]; 
    如果是获取背景色,这种方法在firefox和IE中的返回值还是不一样的,IE中是返回"#ffff99"样子的,而firefox中返回"rgb(238, 44, 34) " 
    值得注意的是:window.getComputedStyle(id,null)这种方式不能设置样式,只能获取,要设置还得写成类似这样id.style.background="#EE2C21"; 
    在IE中CURRENTSTYLE只能以只读方式获取样式. 
    
    用JavaScript修改CSS属性 
    
    只有写原生的javascript了。 
    
    1.用JS修改标签的 class 属性值: 
    
    class 属性是在标签上引用样式表的方法之一,它的值是一个样式表的选择符,如果改变了 class 属性的值,标签所引用的样式表也就更换了,所以这属于第一种修改方法。
    
    更改一个标签的 class 属性的代码是: 
    
    document.getElementById( id ).className = 字符串; 
    document.getElementById( id ) 用于获取标签对应的 DOM 对象,你也可以用其它方法获取。className 是 DOM 对象的一个属性,它对应于标签的 class 属性。字符串 是 class 属性的新值,它应该是一个已定义的CSS选择符。 
    
    利用这种办法可以把标签的CSS样式表替换成另外一个,也可以让一个没有应用CSS样式的标签应用指定的样式。 
    
    举例: 
    
    [javascript] view plaincopy
    <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 属性的代码是: 
    [javascript] view plaincopy
    <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( id ).style.属性名 = 值; 
    document.getElementById( id ) 用于获取标签对应的 DOM 对象,你也可以用其它方法获取。style 是 DOM 对象的一个属性,它本身也是一个对象。属性名 是 Style 对象的属性名,它和某个CSS属性是相对应的。 
    
    说明:这种方法修改的单一的一个CSS属性,它不影响标签上其它CSS属性值。 
  • 相关阅读:
    redis学习(四)
    redis学习(三)
    redis学习(二)
    redis学习(一)
    Maven入门(二)pom.xml和核心概念
    Maven入门(一)
    uni-app 引用
    uni-app 搜索栏
    Vue,组件切换-切换动画
    Vue,组件切换-方式2
  • 原文地址:https://www.cnblogs.com/JerryWang24/p/3769942.html
Copyright © 2011-2022 走看看