Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

javascript - 使用JavaScript将字符串转换为标题大小写(Convert string to title case with JavaScript)

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

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

1 Reply

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...