Transform ALL CAPS to Proper Case using CSS and jQuery

前端 未结 2 1630
灰色年华
灰色年华 2020-12-18 11:24

I\'m trying to do something similar to this thread https://stackoverflow.com/a/3471258/2117845 and I think I want something like Harmen posted with the jquery code and said

2条回答
  •  情话喂你
    2020-12-18 12:00

    I have just modified the code by Harmen to fit your needs. put it inside your JS file.

    jQuery.fn.ucwords = function() {
       return this.each(function(){
          var val = $(this).text().toLowerCase(), newVal = '';
          val = val.split(' ');
    
          for(var c=0; c < val.length; c++) {
             if (c>0 && val[c]=="is" || c>0 && val[c]=="to" || c>0 && val[c]=="the"){
                 newVal+=val[c]+' ';
             } else newVal += val[c].substring(0,1).toUpperCase() + val[c].substring(1,val[c].length) + (c+1==val.length ? '' : ' ');
    
          }
          $(this).text(newVal);
      });
    }
    
    $(document).ready(function(e) {
        $('p.link').ucwords();
    });
    

    HTML for the example

    
    

    and JSFiddle for you http://jsfiddle.net/fqEvX/

提交回复
热议问题