简体   繁体   English

Javascript 事件监听器更改段落文本 onclick 按钮不起作用?

[英]Javascript Event Listener to change paragraph text onclick of button doesn't work?

The <p> text doesn't change when I click the button.当我单击按钮时, <p>文本不会改变。

An alert message shows that output and text variables are correct.一条警报消息显示 output 和文本变量是正确的。 But still the Inner Text of <p> doesn't change.但是<p>的内部文本仍然没有改变。

查看输出图片

Here's the HTML:这是 HTML:

<input type="text" id="msg"><br>
<button onclick="Message()">Submit</button><br>
<h2>Last Message:</h2>
<p class="msg">Message</p>

This is the JS:这是JS:

function Message(){
    var text = document.getElementById("msg").value;
    var output = document.getElementsByClassName("msg")[0].innerHTML;
    
    output = text;
}

You have to set the message directly onto the innerHTML property of the DOM node.您必须将消息直接设置到 DOM 节点的innerHTML属性上。 Fixed example:固定示例:

 function Message() { var text = document.getElementById("msg").value; document.getElementsByClassName("msg")[0].innerHTML = text; }
 <input type="text" id="msg"><br> <button onclick="Message()">Submit</button><br> <h2>Last Message:</h2> <p class="msg">Message</p>

Your defining a variable output to the value of document.getElementsByClassName("msg")[0].innerHTML then you are changing the value of output to be the value of text .您将变量output定义为document.getElementsByClassName("msg")[0].innerHTML的值,然后您将output的值更改为text的值。

Primitives in javascript are passed by value, not by references. javascript 中的原语是按值传递的,而不是按引用传递的。

Change var output = document.getElementsByClassName("msg")[0].innerHTML;更改var output = document.getElementsByClassName("msg")[0].innerHTML;

to document.getElementsByClassName("msg")[0].innerHTML = textdocument.getElementsByClassName("msg")[0].innerHTML = text

If you say something like:如果你说这样的话:

var output = document.getElementsByClassName("msg")[0].innerHTML;

The value of output will just be some kind of string or similar. output 的值将只是某种字符串或类似的。

Then when you change that you just change the value of the string but not the element.然后,当您更改它时,您只需更改字符串的值而不是元素的值。

What you want to do is this:你想要做的是:

var output = document.getElementsByClassName("msg")[0];
output.innerHtml=text;
function Message(){
    var text = document.getElementById("msg").value;
    document.getElementsByClassName("msg").innerHTML = text;
}

You were super close.你超级亲密。

Remove the innerHTML from var output = document.getElementsByClassName("msg")[0].innerHTML;var output = document.getElementsByClassName("msg")[0].innerHTML;中删除innerHTML

and add it here:并在此处添加:

output.innerHTML = text;

So it'll look like this.所以它看起来像这样。

 <input type="text" id="msg"><br> <button onclick="Message()">Submit</button><br> <h2>Last Message:</h2> <p class="msg">Message</p> <script> function Message(){ var text = document.getElementById("msg").value; var output = document.getElementsByClassName("msg")[0]; output.innerHTML = text; } </script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM