gpt4 book ai didi

javascript - 将输入文本格式设置为大写

转载 作者:行者123 更新时间:2023-12-01 04:08:05 25 4
gpt4 key购买 nike

我是初学者,我想构建一个从输入更改开始的事件。输入中输入的文本将自动格式化如下:

  1. 第一个字母必须始终大写;
  2. 所有其他字母都必须小写。

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">

关于javascript - 将输入文本格式设置为大写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41658275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com