简体   繁体   English

如何在 javascript 中使用.textContent 获取用户输入并在屏幕上显示问候语?

[英]How do I take a users input and display a greeting on the screen using .textContent in javascript?

Good morning,早上好,

I have an HTML file that asks the user what their name is followed by a textbox and button.我有一个 HTML 文件,询问用户他们的名字后跟一个文本框和按钮。

Here is my HTML这是我的 HTML

<form>
      <p>What's your name?</p>
      <input id="input" type="text" placeholder="Dwayne Johnson..." />
      <button id="button">Click Me</button>
      <p id="greet"></p>
</form>

I then have my javascript which takes the input (a name) from the user and displays a message on the screen.然后我有我的 javascript,它接受用户的输入(名称)并在屏幕上显示一条消息。

Here is my Javascript这是我的 Javascript

let input = document.getElementById("input");
let button = document.getElementById("button");
let greet = document.getElementById("greet");

// function to greet user (homepage)
button.addEventListener ("click", greetingUser);

function greetingUser() {

    greet.textContent = `Nice to meet you, ${input.value}!`
}

If I enter a name it will display the message for a second and then immediately disappear from the screen.如果我输入名称,它将显示消息一秒钟,然后立即从屏幕上消失。

I appreciate this is quite a simple problem, but I am finding it tricky.我很欣赏这是一个非常简单的问题,但我发现它很棘手。 I am new to DOM manipulation and event handlers.我是 DOM 操作和事件处理程序的新手。 Can someone explain where I am going wrong?有人可以解释我哪里出错了吗?

Thank you so much: :)太感谢了: :)

It's because the page reloads after the button was clicked.这是因为单击按钮后页面会重新加载。 You can use prevent default to avoid that.您可以使用 prevent default 来避免这种情况。

You can add prevent default in your function like this:您可以像这样在 function 中添加阻止默认值:

function greetingUser(event) {
    event.preventDefault()
    greet.textContent = `Nice to meet you, ${input.value}!`
}

The problem is you used a <form> , that form will submit and it cause the problem, if you need only display the name don't use form like:问题是您使用了<form> ,该表单将提交并导致问题,如果您只需要显示名称,请不要使用如下表单:

 let input = document.getElementById("input"); let button = document.getElementById("button"); let greet = document.getElementById("greet"); // function to greet user (homepage) button.addEventListener("click", greetingUser); function greetingUser() { greet.textContent = `Nice to meet you, ${input.value}!` }
 <p>What's your name?</p> <input id="input" type="text" placeholder="Dwayne Johnson..." /> <button id="button">Click Me</button> <p id="greet"></p>


If, on the other hand, you need to display the name and then send the form with another button you have to use preventDefault() like:另一方面,如果您需要显示名称,然后使用另一个按钮发送表单,则必须使用preventDefault() ,例如:

 let input = document.getElementById("input"); let button = document.getElementById("button"); let greet = document.getElementById("greet"); // function to greet user (homepage) button.addEventListener("click", greetingUser); function greetingUser(e) { e.preventDefault(); greet.textContent = `Nice to meet you, ${input.value}!` }
 <form> <p>What's your name?</p> <input id="input" type="text" placeholder="Dwayne Johnson..." /> <button id="button">Click Me</button> <p id="greet"></p> </form>

You don't have to change your mark-up, but inserting a call to the preventDefault() method on the event object should prevent the form from being submitted.您不必更改标记,但在事件 object 上插入对 preventDefault() 方法的调用应该会阻止提交表单。

let input = document.getElementById("input");
let button = document.getElementById("button");
let greet = document.getElementById("greet");

// function to greet user (homepage)
button.addEventListener ("click", greetingUser);

function greetingUser(event) {
  event.preventDefault();
  greet.textContent = `Nice to meet you, ${input.value}!`
}

暂无
暂无

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

相关问题 Javascript:如何让用户输入数据并将其转换为对象? - Javascript: how do I take my users input data and turn it into an object? 该元素不会接受 textContent 输入 - The element will not take a textContent input 如何使用.TextContent显示文本? - How to Display the Text using .TextContent? 如何使用 javascript 在页面中输入并在 html 中显示输入? - How to take input in a page using javascript and display the input in the html? 我如何接受用户输入并使用它来搜索 api 反应? - How do I take a users input and use it to search an api in react? 如何获取2个数字输入字段的值,然后使用Jquery进行除法并将结果显示给另一个输入? - How do i take values of 2 input fields which are numbers, then divide using Jquery and display the result to another input? 如何检索textContent并在复选框输入中显示 - How to retrieve textContent and display on checkbox input 如何获取用户输入并使用JavaScript链接到相应的HTML页面? - How do I take user input and link to corresponding HTML page using JavaScript? 如何使用由 javascript 控制的 php 进行复选框输入? - how do you take checkbox input using php that is controlled by javascript? 如何获取分配给带有 textContent 的变量的内容,以作为 javascript 中我的播放器输入的参数? - how can i get what is being assigned to a variable with textContent to act as an argument for my player input in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM