• style、 currentStyle、 runtimeStyle区别分析


    1、obj.style只能获得内嵌样式(inline Style)就是写在Tag里面的,他访问不到那些链接的外部css和在head中用<style>声明的style。
    所以必须认识到在那些使用外部Css文件的页面中,如果用style赋值,如obj.style=“color:red”;显然效果是正确的,其中的奥秘确是只是在该对象的tag上多添加了一个style属性,按照由小到大的优先级呈现罢了。

    2、obj.currentStyle就强大多了,他能够获取关于这个节点所有位置的style,但是他也按照优先级,说穿了就是显示的是什么他就是指向哪一个style,如下代码字体优先是显示blue的,那currentStyle.color就是blue,当然此时style.color也会是blue。

     1 <html>
     2 <head>
     3 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     4 <title>testStyle</title>
     5 <style>
     6 .lala{
     7  color:yellow; 
     8 }
     9 </style>
    10 </head>
    11 <body>
    12 <div id="tt" style="color:blue;" class="lala">1111</div>
    13 </body>
    14 <script>
    15 alert(document.getElementById("tt").currentStyle.color);
    16 </script>
    17 </html> 

    若去掉以上<div>中的style为<div id="tt" class="lala">1111</div>,那么currentStyle.color就根据优先级变成了yellow,但是此时style.color为空。

    3、runtimeStyle简单的说就是你可以对一个节点的样式赋值,他将成为最高优先级的节点样式。
    如:

     1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
     5 <style>
     6 .lala{
     7 color:yellow; 
     8 }</style>
     9 </head>
    10 <body>
    11 <div id="tt" style="color:blue;" class="lala">1111</div>
    12 </body>
    13 <script>
    14 document.getElementById("tt").runtimeStyle.color="black";
    15 alert(document.getElementById("tt").currentStyle.color);
    16 alert(document.getElementById("tt").runtimeStyle.color);
    17 alert(document.getElementById("tt").style.color);
    18 </script>
    19 </html> 

    此时页面显示字的颜色是runtimeStyle赋值后的black。但是只有currentStyle.color和runtimeStyle本身能够取到这个值,style.color取到的依然是tag中的blue。

  • 相关阅读:
    webpack搭建vue项目,实现脚手架功能
    【学习笔记】JS设计模式总结
    【学习笔记】JS经典异步操作,从闭包到async/await
    【学习笔记】深入理解async/await
    【学习笔记】剖析MVVM框架,简单实现Vue数据双向绑定
    【学习笔记】node.js重构路由功能
    使用react-app-rewired和customize-cra 个性化配置
    axios 生产环境和开发环境 ip 切换(修改文件配置)
    socketio 更改requsturl 实现nginx 代理多个服务端
    vue-json-editor 简单实现
  • 原文地址:https://www.cnblogs.com/luzhiyuan/p/2736276.html
Copyright © 2020-2023  润新知