KeyPress事件处理程序未按预期反映输入文本

huangapple go评论115阅读模式
英文:

KeyPress event handler does not mirror input text as expected

问题

我有一个文本区域和一个段落,在段落中,我会将用户在文本区域中输入的文本镜像显示出来。在段落下方有一个代码格式化工具,当用户在文本区域中的当前行以四个空格或三个反引号开始时,应该显示该工具。我使用键盘按键的事件处理程序来实现此行为。然而,当我在文本区域中输入一个字符时,它不会立即在段落中镜像,直到我再输入另一个字符。我应该怎么做才能让我的JavaScript文件立即获取文本并在预览段落中进行镜像?我尝试过 OnChange,但结果是一样的,字符直到我再输入另一个字符才会被镜像。

文本区域

<textarea onkeypress="duplicate()" class="form-control" rows="10" cols="10" id="text"></textarea>

JavaScript文件

function duplicate() {
    //定义代码块部分
    let tool = document.getElementById("codeTool");
    //获取要镜像的段落
    let para = document.getElementById("content");
    //获取在文本区域中键入的文本
    let texta = document.getElementById("text").value;
    //根据换行符分割行
    let lines = texta.split("\n");
    let cline = lines[lines.length - 1];
    if (cline.startsWith("```")) {
        tool.style.visibility = "visible";
    }
    para.innerText = texta;
}
英文:

I have a text area and a paragraph where I mirror the text entered by the user in the text area to the paragraph. Below that paragraph there is a code formatting tool that should be displayed when the user starts the current line in the text area with either four spaces or three back ticks. I use the event handler for the key press to achieve this behavior. However when I enter a character inside the text area then it won't be mirrored instantly in the paragraph until I enter another character. What should I do to my Javascript file so that it immediately gets the text and mirrors on the preview paragraph? I have tried OnChange but the result is the same, character doesn't get mirrored until I enter another character.

The TextArea

&lt;text area onkeypress=&quot;duplicate()&quot; class=&quot;form-control&quot; rows=&quot;10&quot; cols=&quot;10&quot; id=&quot;text&quot; &gt;&lt;/textarea&gt;

The JavaScript file

function duplicate() {
    //define the code block section
    let tool = document.getElementById(&quot;codeTool&quot;);
    //get the paragraph to mirror
    let para = document.getElementById(&quot;content&quot;);
    //get the text typed in the text area
    let texta = document.getElementById(&quot;text&quot;).value;
    //split the lines based on the newline character
    let lines = texta.split(&quot;\n&quot;);
    let cline = lines[lines.length - 1];
    if (cline.startsWith(&quot;```&quot;)) {
        tool.style.visibility = &quot;visible&quot;;
    }
    para.innerText = texta;

} 

答案1

得分: 1

你可以尝试使用 onkeyup

onkeydownonkeypress 在文本区域的值更新之前触发。

function duplicate() {

    // 获取要镜像的段落
    let para = document.getElementById("content");
    // 获取在文本区域中键入的文本
    let texta = document.getElementById("text").value;
    // 根据换行符拆分行
    let lines = texta.split("\n");
    let cline = lines[lines.length - 1];
    if (cline.startsWith("```")) {
        tool.style.visibility = "visible";
    }

    para.innerText = texta;

}
<textarea onkeyup="duplicate()" class="form-control" rows="10" cols="10" id="text"></textarea>

<p id="content"></p>
英文:

You can try using onkeyup.

onkeydown and onkeypress fire before value of textarea is updated.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function duplicate() {

    //get the paragraph to mirror
    let para = document.getElementById(&quot;content&quot;);
    //get the text typed in the text area
    let texta = document.getElementById(&quot;text&quot;).value;
    //split the lines based on the newline character
    let lines = texta.split(&quot;\n&quot;);
    let cline = lines[lines.length - 1];
    if (cline.startsWith(&quot;```&quot;)) {
        tool.style.visibility = &quot;visible&quot;;
    }

    para.innerText = texta;

} 

<!-- language: lang-html -->

&lt;textarea onkeyup=&quot;duplicate()&quot; class=&quot;form-control&quot; rows=&quot;10&quot; cols=&quot;10&quot; id=&quot;text&quot; &gt;&lt;/textarea&gt;

&lt;p id=&quot;content&quot;&gt;&lt;/p&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月11日 13:51:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76659007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定