zoukankan      html  css  js  c++  java
  • js连字符转驼峰


    css 中经常有类似 background-image 这种通过 - 连接的字符,通过 javascript 设置样式的时候需要将这种样式转换成 backgroundImage 驼峰格式,请完成此转换功能
    1. 以 - 为分隔符,将第二个起的非空单词首字母转为大写
    2. -webkit-border-image 转换后的结果为 webkitBorderImage

    function cssStyle2DomStyle(sName) {
       return sName.replace(/^-/, '').replace(/-(w)(w+)/g, function(a, b,c){
       return b.toUpperCase() + c.toLowerCase();
      });
    }

    function cssStyle2DomStyle(sName) {     var arr = sName.split('-');     for(var i = 0; i
    < arr.length; i++){         if(arr[i] == ''){             arr.splice(i,1);             i--;         }         else{             if(i >= 1){                 arr[i] = arr[i].substring(0,1).toUpperCase() + arr[i].substring(1);             }         }     }     return arr.join(''); } 不用正则表达式,substring()会把字符串看成一个个字符
  • 相关阅读:
    寒假周总结一
    1657. Determine if Two Strings Are Close
    1656. Design an Ordered Stream
    695. Max Area of Island (BFS)
    695. Max Area of Island (DFS)
    Daily Coding Problem: Problem #713
    939. Minimum Area Rectangle
    259. 3Sum Smaller
    29. Divide Two Integers
    16. 3Sum Closest
  • 原文地址:https://www.cnblogs.com/CoderTony/p/7707211.html
Copyright © 2011-2022 走看看