[英]JavaScript Capitalizing First Letter of a String
I have written the code within the HTML file and it doesn't work typing lower or console.log(lower) does not display anything.我已经在 HTML 文件中编写了代码,但输入 lower 或 console.log(lower) 没有显示任何内容。
However when I type the same code但是当我输入相同的代码时
const lower = 'this is a lowercase string'
const upper = lower.charAt(0).toUpperCase() + lower.substring(1);
directly into the JS console it works perfectly fine.直接进入 JS 控制台它工作得很好。
Here is script to help you.这是可以帮助您的脚本。
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
}
With this prototype, you can make first letter as Uppercase.使用此原型,您可以将第一个字母设为大写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.