[英]How to display elements in an html page using javascript?
I have a set of hardcoded html elements.我有一组硬编码的 html 元素。
<!-- Reciever Message-->
<div class="media w-50 ml-auto mb-3">
<div class="media-body">
<div class="bg-primary rounded py-2 px-3 mb-2">
<p class="text-small mb-0 text-white">Test chat</p>
</div>
</div>
</div>
I'm trying to generate this set of elements with a button click and add the text typed in the input box into the <p>
tag.我正在尝试通过单击按钮生成这组元素,并将在输入框中键入的文本添加到
<p>
标记中。 The code for the button and the text input box is as follows.按钮和文本输入框的代码如下。
<form action="#" class="bg-light">
<div class="input-group">
<input type="text" placeholder="Type a message" aria-describedby="button-addon2" class="form-control rounded-0 border-0 py-4" id="message">
<div class="input-group-append">
<button id="button-addon2" type="submit" class="btn btn-link" onclick="addText()"> <i class="fa fa-paper-plane"></i></button>
</div>
</div>
</form>
The function for the button click is,用于按钮单击的 function 是,
function addText() {
var p = document.createElement("p");
p.className ="text-small mb-0 text-white"
var inputValue = document.getElementById("message").value;
var text = document.createTextNode(inputValue);
p.appendChild(text);
if (inputValue === '') {
alert("You must write something!");
} else {
var div = document.createElement("div");
div.className = "media w-50 ml-auto mb-3";
var div2 = document.createElement("div");
div2.className = "media-body";
var div3 = document.createElement("div");
div3.className = "bg-primary rounded py-2 px-3 mb-2";
div3.id = "receive-text";
div3.appendChild(p);
}
document.getElementById("message").value = "";
}
When i click the button nothing is happening?当我点击按钮时什么都没有发生? Am i doing something wrong?
难道我做错了什么?
I tweaked the code to make the desired output我调整了代码以制作所需的 output
function addText() {
var p = document.createElement('p');
p.className = 'text-small mb-0 text-white';
var inputValue = document.getElementById('message').value;
var text = document.createTextNode(inputValue);
p.appendChild(text);
if (inputValue === '') {
alert('You must write something!');
} else {
var div = document.createElement('div');
div.className = 'media w-50 ml-auto mb-3';
var div2 = document.createElement('div');
div2.className = 'media-body';
div.appendChild(div2);
var div3 = document.createElement('div');
div3.className = 'bg-primary rounded py-2 px-3 mb-2';
div3.id = 'receive-text';
div2.appendChild(div3);
div3.appendChild(p);
document.body.appendChild(div);
}
document.getElementById('message').value = '';
}
You were not using appendChild.您没有使用 appendChild。 That was the only mistake.
那是唯一的错误。
I see that you are creating the element div3
which you appends the p
that you are talking about, but I don't see you appending them to the DOM.我看到您正在创建元素
div3
,您将其附加到您正在谈论的p
中,但我没有看到您将它们附加到 DOM 中。
So you need to do所以你需要做
document.body.appendChild(div3)
or或者
document.getElementById("#idOfDiv3Parentelement").appendChild(div3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.