简体   繁体   English

当您使用IIFE按下Enter键时,我想显示文本

[英]I want to display the text when you hit enter using IIFE

I have a text box that takes input and when i hit enter it stores the output and displays in an unordered list format. 我有一个接受输入的文本框,当我按Enter键时,它存储输出并以无序列表格式显示。 The function without IIFE works when i use onclick event. 当我使用onclick事件时,没有IIFE的功能将起作用。 However, with IIFE its not functioning. 但是,对于IIFE,它无法正常工作。 Please help. 请帮忙。

<html>

<head>
<title>messagewithenter</title>
</head>

<body>

Message:
<input type="text" name="message" id="message">
<input type="submit" value="add" id="submitenter" style="display: none">

<ul id="list"></ul>

<script>    

    (function () {

        var link = document.getElementById("submitenter");
        link.addEventListener("click", function () {
            document.onkeypress = enter;
            function enter(e) {
                if (e.which == 13 || e.keyCode == 13) {
                    addMessage();
                }
            }
        });
        function addMessage() {
            var message = document.getElementById("message").value;
            var output = "<li>" + message + "<li" + "<br>";

            document.getElementById("list").innerHTML += output;

        }
    }());

   </script>

</body>

 </html>

You were binding the event on click event of an invisible element, simply make it 您将一个隐形元素的click事件绑定到该事件上,只需使其

document.onkeypress = enter;

function enter(e) {
  if (e.which == 13 || e.keyCode == 13) {
    addMessage();
  }
}

Demo 演示版

 document.onkeypress = enter; function enter(e) { if (e.which == 13 || e.keyCode == 13) { addMessage(); } } function addMessage() { var message = document.getElementById("message").value; var output = "<li>" + message + "<li" + "<br>"; document.getElementById("list").innerHTML += output; } 
 Message: <input type="text" name="message" id="message"> <input type="submit" value="add" id="submitenter" style="display: none"> <ul id="list"></ul> 

    link.addEventListener("click", function () {
        document.onkeypress = enter;
        ...

This code is flawed because if it works, it would reset the document.onkeypress event handler every time you click in the input box. 该代码存在缺陷,因为如果它可以正常工作,则每次您在输入框中单击时,它将重置document.onkeypress事件处理程序。

It's also unnecessary, as you can set that onkeypress handler without needing the click event. 这也是不必要的,因为您可以设置onkeypress处理程序而不需要click事件。 The whole thing can be written like this: 整个事情可以这样写:

document.onkeypress = function(e) {
    if (e.which == 13 || e.keyCode == 13) {
        addMessage();
    }
}

No need for the click handler at all. 完全不需要点击处理程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当您按下Enter键时,我希望我的表格继续进行 - I want my form to progress when you hit the enter key 当我在文本框中输入值并点击提交时,我希望使用ajax在文本框下方输入该值 - When i enter value into textbox and hit submit i want that value below the textbox using ajax 当我在 form_for 中的 text_field_tag 之一中按 Enter 时,如何禁用发出 POST 请求 - How to disable making a POST request when I hit Enter in one of text_field_tag in a form_for 当我在文本框中输入单词并按空格键时,如何将输入的单词变成一个单独的短语,如何创建文本框? - How can I create a text box when I enter words in text box and hit spacebar it has to make entered words into a separate phrase? 当用户按下回车键时,自动在文本区域/文本中插入消息 - Auto insert message in textarea/text when user(s) hit the enter 如何使搜索仅在按 Enter 键时才起作用? - How I make the search only work when I hit enter? 我想通过下拉选择在文本框中输入文本 - i want to enter text in textbox by dropdown selection jQuery autocomplete。当您具有autocomplete并按“ enter”时,我不想搜索,实际上它会搜索 - jquery autocomplete .When you have the autocomplete and press 'enter',i do not want to search ,actually it will do search HTML表单 - 当我点击输入时刷新页面! - HTML form - when I hit enter it refreshes page! 当我按下Enter键时,一切都会变回原位(javascript和html) - Everything changes back when i hit enter (javascript and html)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM