首页 > 解决方案 > 按键“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:&nbsp; <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:&nbsp;";
              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:&nbsp;";
          let child = document.getElementById("textbox").appendChild(z);
          setEndOfContenteditable(child)
          enterPressed = 0;

      }
  };
div {
  border: 1px solid black;
}
<div contenteditable id="textbox"></div>

标签: javascriptstring

解决方案


将某事物的首字母大写的简单方法如下:

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)

推荐阅读