JS小技巧

JS操作伪元素

CSS代码:

#myId:before {
    content: "hello world!";
    display: block;
    width: 100px;
    height: 100px;
    background: red;
}

JS 代码:

var myIdElement = document.getElementById("myId");
var beforeStyle = window.getComputedStyle(myIdElement, ":before");
console.log(beforeStyle); // [CSSStyleDeclaration Object]
console.log(beforeStyle.width); // 100px
console.log(beforeStyle.getPropertyValue("width")); // 100px
console.log(beforeStyle.content); // "hello world!"

这样就获取到为元素的样式了,那么改如何操作呢?

posted @ 2017-03-22 09:40  郭老猫  阅读(93)  评论(0编辑  收藏  举报