javascript - 按键“enter”时冒号空格后的第一个单词大写
问题描述
我正在做一个小项目,当我按下回车键时,我得到正常的“R:”,当再次按下回车键时,我得到粗体“ M: ”,后面都是空格。
被访者是“R”,主持人会提问,所以需要加粗。此外,我尝试引入语音输入来简化流程,现在我面临的问题是每当我语音输入时,句子的第一个字母是小写的,理想情况下,第一个字母应该是大写的。
我尝试使用正则表达式,但失败了,使用 JS 将“R:”或“**M:**”之后的第一个字母变为大写的最佳方法是什么?
以下是输出“R:”和“M:”的按键代码,当我按下回车键时,
function setEndOfContenteditable(contentEditableElement) {
var range, selection;
if (document.createRange) //Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange(); //Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement); //Select the entire contents of the element with the range
range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection(); //get the selection object (allows you to change selection)
selection.removeAllRanges(); //remove any selections already made
selection.addRange(range); //make the range you have just created the visible selection
} else if (document.selection) //IE 8 and lower
{
range = document.body.createTextRange(); //Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement); //Select the entire contents of the element with the range
range.collapse(false); //collapse the range to the end point. false means collapse to end rather than the start
range.select(); //Select the range (make it the visible selection
}
}
var enterPressed = 0;
window.onkeypress = function (e) {
var keyCode = (e.keyCode || e.which);
if (keyCode === 13) {
if (enterPressed === 0) {
e.preventDefault();
var z = document.createElement('p'); // is a node
z.innerHTML = "<br><b>M: <b>";
let child = document.getElementById("textbox").appendChild(z);
setEndOfContenteditable(child)
enterPressed++;
} else if (enterPressed === 1) {
e.preventDefault();
var z = document.createElement('p'); // is a node
z.innerHTML = "<br>R: ";
let child = document.getElementById("textbox").appendChild(z);
setEndOfContenteditable(child)
enterPressed++;
enterPressed = 0;
}
} else if (keyCode === 92) {
e.preventDefault();
var z = document.createElement('p'); // is a node
z.innerHTML = "<br>R: ";
let child = document.getElementById("textbox").appendChild(z);
setEndOfContenteditable(child)
enterPressed = 0;
}
};
div {
border: 1px solid black;
}
<div contenteditable id="textbox"></div>
解决方案
将某事物的首字母大写的简单方法如下:
let text = 'lorem ipsum dolor sit amet'
text = text.charAt(0).toUpperCase() + text.slice(1)
// text => 'Lorem ipsum dolor sit amet'
要执行上述操作,您的 HTML 格式必须允许您定位 R: 和 M: 之后的文本:
例如:
R: <span class="body">lorem ipsum dolor sit amet</span>
const body = document.querySelector('.body')
const text = body.innerText
body.innerText = text.charAt(0).toUpperCase() + text.slice(1)
推荐阅读
- python - DecisionTreeClassifier model.fit(X, y) 命令返回错误
- html - 如何在 CSS 中格式化表格,以使某些列的行向下移动其行高一半的距离?
- graphics - 使用 FBX SDK,如何在单个 fbx 文件中提取多个动画?
- typescript - 如何在 TypeScript 中配置更严格的类型检查?
- matlab - 如何在matlab中制作向量和数字之间的字典
- python - 网页抓取 Beautifulsoup
- r - 如何使用 dplyr 在 r 平台中将日期转换为月份
- google-sheets - 在 Google Sheet 或 Excel 中按 ID 分组和按 SUB UNIQUE ID 计数
- azure - Azure App Service:具有三个容器的多容器应用程序(如何处理)
- java - 获取 java.net.SocketTimeoutException:在 AWS CodeBuild 期间连接超时