Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there a simple way to convert a string to title case?(有没有简单的方法可以将字符串转换为标题大小写?)

Eg john smith becomes John Smith .(例如, john smith John Smith成为John Smith 。) I'm not looking for something complicated like John Resig's solution , just (hopefully) some kind of one- or two-liner.(我并不是在寻找像John Resig的解决方案那样复杂的东西,只是(希望)某种一线或两线。)   ask by MDCore translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
600 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try this:(尝试这个:)

function toTitleCase(str) { return str.replace( /\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); } ); } <form> Input: <br /><textarea name="input" onchange="form.output.value=toTitleCase(this.value)" onkeyup="form.output.value=toTitleCase(this.value)"></textarea> <br />Output: <br /><textarea name="output" readonly onclick="select(this)"></textarea> </form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...