美文网首页
css函数-设置或读取对象的属性

css函数-设置或读取对象的属性

作者: 安好每个你 | 来源:发表于2018-03-25 02:33 被阅读12次

HTML

<div class="container">
    <div id="box" class="box">
        <div class="btns">
            <button onclick="getStyle()">Get Style</button>
            <button onclick="setStyle()">Set Style</button>
            <button onclick="default_style()">Default Style</button>
        </div>
    </div>
    <div class="box"></div>
</div>

JS

var box = document.getElementById('box');
    function getStyle() {
        var background_color = window.getComputedStyle(box, null).getPropertyValue("background-color");
        var width = window.getComputedStyle(box, null).getPropertyValue("width");
        var height =  window.getComputedStyle(box, null).getPropertyValue("height");
        alert("width: "+width + '\n' +  "height: "+height + '\n' + "background-color: "+background_color)
    }
    function setStyle() {
        box.style.width = 330 + 'px';
        box.style.height = 100 + 'px';
        box.style.backgroundColor = 'rgb(239, 248, 255)';
        box.style.borderColor = 'blue';
        for (var i=0; i<3; i++) {
            document.getElementsByTagName('button')[i].style.backgroundColor = 'blue'
        }
    }
    function default_style() {
        box.style.width = 400 + 'px';
        box.style.height = 200 + 'px';
        box.style.backgroundColor = 'rgb(254, 244, 235)';
        box.style.borderColor = 'orangered';
        for (var i=0; i<3; i++) {
            document.getElementsByTagName('button')[i].style.backgroundColor = 'orangered'
        }
    }

getComputedStyle方法给出应用活动样式表后的元素的所有CSS属性的值,返回CSSStyleDeclaration表示一个css属性键值对的集合,getPropertyValue是其的方法之一,返回属性值,以下示例返回的就是属性值

    var style1 = getComputedStyle(target, null).getPropertyValue('background-color');

alert()中换行 +'\n'+

这个函数写的好,这样简单了好多

function css(obj, attr, value)
{
    switch (arguments.length)
    {
        case 2:
            if(typeof arguments[1] == "object")
            {    //二个参数, 如果第二个参数是对象, 批量设置属性
                for (var i in attr)obj.style[i] = attr[i]
            }
            else
            {    //二个参数, 如果第二个参数是字符串, 读取属性值
                return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, null)[attr]
            }
            break;
        case 3:
            //三个参数, 单一设置属性
            obj.style[attr] = value;
            break;
        default:
            alert("参数错误!")
    }
}

相关文章

  • css函数-设置或读取对象的属性

    HTML JS getComputedStyle方法给出应用活动样式表后的元素的所有CSS属性的值,返回CSSSt...

  • jQuery_css

    对于jQuery的css属性方法: 它有读取css样式和设置css样式的功能; 一.读取 我们通过jQuery对象...

  • JS高级特性简单实现—贪吃蛇

    搭建页面 index.css 分析对象 游戏对象 蛇对象 食物对象 食物对象 1.创建构造函数Food,设置属性 ...

  • Object.defineProperty()

    getter 是读取对象属性时调用的函数setter 是写入对象属性时调用的函数

  • javascript常见对象属性方法

    Array 对象 对象属性: constructor 返回对创建此对象的数组函数的引用。length 设置或返回数...

  • prop()

    注意:prop()函数的所有"设置属性"操作针对的是当前jQuery对象所匹配的每一个元素;所有"读取属性"的操作...

  • 二十三(十三)ObjectSetDouble

    ObjectSetDouble 函数设置相应对象属性的值。对象属性必须是双类型的。这个函数有两个变体。设置属性值,...

  • 二十三(十五)ObjectSetString

    ObjectSetString 函数设置相应对象属性的值。对象属性必须是字符串类型。这个函数有两个变体。设置属性值...

  • jq-基础

    1.入口函数 $(funtion(){这里写内容 }) 2.获取css的属性值 元素.css("属性") 设置...

  • 三、获取和设置属性值、操作属性·

    .attr()用来读取或设置指定的属性,需要读取属性时,在小括号中指定属性的名称。 one为读取位置: .addC...

网友评论

      本文标题:css函数-设置或读取对象的属性

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