将输入文本格式化为大写 [英] Format input text to uppercase
本文介绍了将输入文本格式化为大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是乞gg,我想建立一个从更改输入开始的事件.输入中输入的文本将自动设置为以下格式:
I'm begginer and I would like to build an event that started on change of input. The text entered in the input would be automatically formatted as follows:
- 首字母必须始终为大写;
- 所有其他字母必须为小写.
function formating() {
var nameOfPerson = document.getElementById("nameOfPerson").textContent;
var nameOfPerson = nameOfPerson[0].toUpperCase() + (nameOfPerson - nameOfPerson[0]);
document.getElementById("nameOfPerson").textContent = nameOfPerson;
}
<input type="text" id="nameOfPerson" onchange="formatting()" placeholder="type your name">
推荐答案
尝试一下:
function formatting() {
var nameOfPerson = this.value;
if (nameOfPerson.length > 0) {
nameOfPerson = nameOfPerson[0].toUpperCase() + nameOfPerson.substr(1).toLowerCase();
this.value = nameOfPerson;
}
}
<input type="text" id="nameOfPerson" onchange="formatting.call(this)" placeholder="type your name">
这篇关于将输入文本格式化为大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文