Javascript / convert CSS style string into JS object

后端 未结 10 1040
粉色の甜心
粉色の甜心 2021-01-04 08:41

We\'d like to convert a CSS style entered as string into a JS object.

E.g.,

 var input = \" border:solid 1px; color:red \";

expec

10条回答
  •  甜味超标
    2021-01-04 09:20

    All the answers seem to need a lot of splitting- why not do a match and get all the pairs in one go?

    function cssSplit(str){
        var O= {},
        S= str.match(/([^ :;]+)/g) || [];
        while(S.length){
            O[S.shift()]= S.shift() || '';
        }
        return O;
    }
    

提交回复
热议问题