zoukankan      html  css  js  c++  java
  • js获取元素的外链样式

    一般给元素设置行内样式,如<div id="div1" style="500px;"></div>。如要获取它的样式,即可document.getElementById("div1").style.width来获取或设置。但是如果样式是在外链link中的或者是页面的非行内样式,就获取不到了。

    在标准浏览器中可以通过window.getComputedStyll(obj,null)[property]来获取外链样式,但是在ie浏览器中则是通过obj.currentStyle来获取。

    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    p{width:500px;line-height:30px;}
    </style>
    <script src="/jquery/jquery-1.11.1.min.js">
    </script>
    <script>
    function getstyle(obj,property){
    if(obj.currentStyle){
    return obj.currentStyle[property];
    }else if(window.getComputedStyle){
    return document.defaultView.getComputedStyle(obj,null)[property];//或者也可以通过window.getComputedStyle来获取样式
    }
    return null;
    }
    $(document).ready(function(){
      $("p").click(function(){
       alert(getstyle(this,"width"));
      });
    });
    </script>
    </head>
    <body>
    <p style="750px;">如果您点击我,我会消失。</p>
    <p>点击我,我会消失。</p>
    <p>也要点击我哦。</p>
    </body>
    </html>
    人生短短几十年,要在有限的生命里多做店有意义的事情。莫要让自己迎合别人的眼光活着。随心而为,听从心的声音。讨好自己,悠哉悠哉!
  • 相关阅读:
    js 函数柯里化和闭包的使用
    人员轨迹运动效果
    D3绘制柱状图
    D3选择元素和绑定数据
    h5--uni.setNavigationBarColor 动态修改顶部背景颜色
    友链
    canvas 整个透明
    JS将某个数组分割为N个对象一组(如,两两一组,三三一组等)
    小程序正则表达式
    微信小程序--设置和获取剪切板内容
  • 原文地址:https://www.cnblogs.com/jiaojiaome/p/4222831.html
Copyright © 2011-2022 走看看