With jQuery, how do I capitalize the first letter of a text field while the user is still editing that field?

后端 未结 21 2499
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-27 16:26

I\'m looking for an example of how to capitalize the first letter of a string being entered into a text field. Normally, this is done on the entire field with a function, r

21条回答
  •  没有蜡笔的小新
    2020-11-27 16:48

    My personal favorite when using jQuery is short and sweet:

    function capitalize(word) {
       return $.camelCase("-" + word);
    }
    

    There's a jQuery plugin that does this too. I'll call it... jCap.js

    $.fn.extend($, { 
        capitalize: function() {
            return $.camelCase("-"+arguments[0]); 
        } 
    });
    

提交回复
热议问题