Я делаю небольшой проект, в котором, когда я нажимаю клавишу ввода один раз, я получаю нормальное «R:», а когда снова нажимаю клавишу ввода, я получаю жирный шрифт « M: », за которым следует пробел.
Ответчик - «R», и модератор будет задавать вопросы, поэтому он должен быть жирным. Кроме того, я попытался ввести голосовой набор текста, чтобы упростить процесс, и теперь проблема, с которой я сталкиваюсь, заключается в том, что всякий раз, когда я озвучиваю текст, первая буква предложения будет строчной, в идеале первая буква должна быть в верхнем регистре.
Я пытался использовать регулярные выражения, но это было неудачно, что было бы лучшим способом сделать первую букву после заглавных букв "R:" или "** M: **", используя JS?
Ниже приведен код нажатия клавиши, который выводит «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>