程序员鸡皮

文章 分类 评论
95 3 22

站点介绍

一名PHP全栈程序员的日常......

JavaScript中将style的String类型转换成Object类型

abzzp 2024-09-01 610 1条评论 前端 JavaScript

首页 / 正文
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......

发布于2024-07-04

在React开发中,我们或许经常遇到要将font-size:20px;转换成对象类型{fontSize:"20px"},如下我自己写了个类,正则匹配-后面的第一个字为大写字母,并且去掉-,然后将:后的属性转换为字符串类型,代码如下

function styleStrToObject(styleStr){
    const obj = {};
    const s = styleStr.toLowerCase().replace(/-(.)/g,function(m,g){
        return g.toUpperCase();
    }).replace(/;\s?$/g,"").split(/:|;/g)

    for(var i=0;i<s.length;i+=2){
        obj[s[i].replace(/\s/g,"")] = s[i+1].replace(/^s+|\s+$/g,"");
    }

    return obj
}

export default styleStrToObject

感谢大家观看,我们下次见

评论(1)

  1. abzzp 管理员 2024-09-02 13:34 回复

    @React实战爱彼迎项目(二) - 程序员鸡皮

    哪里有问题了,报错了吗?

最新评论

  • fintechbase

    It's not my first time to pay a visit this web page, i am visiting this website dailly and get good facts from here every day.

  • fintechbase

    Hello, i think that i saw you visited my site so i got here to go back the want?.I'm trying to find things to improve my web site!I suppose its adequate to use a few of your concepts!!

  • fintechbase

    Touche. Outstanding arguments. Keep up the great effort.

  • fintechbase

    Wow, this post is good, my younger sister is analyzing these things, therefore I am going to let know her.

  • digital banking

    There's certainly a lot to learn about this topic. I like all the points you've made.

  • 觀景碼頭

    所有文章都令人印象深刻。由衷感谢 带来的灵感。

  • 圣巴托罗梅奥

    你 百分百 分享经验。坚持!

  • 孤懸外海

    不常看到, 这么高质量的内容。谢谢。

  • abzzp

    @十二使徒岩 哪张图片?

  • 十二使徒岩

    照片令人惊艳。继续保持 心情。

日历

2026年01月

    123
45678910
11121314151617
18192021222324
25262728293031

文章目录

站点公告
本站是作为记录一名北漂程序员编程学习以及日常的博客,欢迎添加微信BmzhbjzhB咨询交流......
点击小铃铛关闭
配色方案