admin管理员组文章数量:1325236
I'm trying to add (and display) divs when a user presses a button, I'm also using css grids. I've had a look here and all the solutions advise using angular js or node js.
I've recently (last week) learnt html, css and javascript and I'm pressed for time hence I'm asking if is it possible to create these divs without angular js or node js?
I'm trying to add (and display) divs when a user presses a button, I'm also using css grids. I've had a look here and all the solutions advise using angular js or node js.
I've recently (last week) learnt html, css and javascript and I'm pressed for time hence I'm asking if is it possible to create these divs without angular js or node js?
Share Improve this question asked Jun 20, 2018 at 13:59 MingLiMingLi 531 silver badge9 bronze badges 8- 1 Yes it is possible – Vignesh Raja Commented Jun 20, 2018 at 14:00
- yeah its just <div></div> – L_Church Commented Jun 20, 2018 at 14:01
- 4 People were creating dynamic HTML long, long before Angular was even dreamed of. And Node really has nothing at all to do with it. – Pointy Commented Jun 20, 2018 at 14:01
- @L_Church I know that method I, that's hard coding it. I needed the user to be able to add columns to a grid I created at the press of a button. I got the answer. – MingLi Commented Jun 20, 2018 at 14:09
- html is not code it's markup – L_Church Commented Jun 20, 2018 at 14:11
5 Answers
Reset to default 4You can create HTML elements by using createElement
It's acutally pretty simple:
function add() {
// Create element; can be whatever you want, e. g. div, h1, p, img...
var div = document.createElement('div');
// Set some attributes
div.style.width = '200px';
div.style.height = '200px';
div.style.backgroundColor = 'red';
// Append the div to the body
document.body.appendChild(div);
}
<button onclick="add()">Add div</button>
There are lots of ways to do it.
The obvious one is the mysteriously-named
createElement
, usually bined withappendChild
and/orinsertBefore
:parent.appendChild(document.createElement("div"));
You can replace the contents of a parent element by assigning a string containing HTML to its
innerHTML
:parent.innerHTML = "<div></div>";
#2 replaces the parent's content. To add to it, you can use
insertAdjacentHTML
, again with a string containing HTML:parent.insertAdjacentHTML("beforeend", "<div></div>");
Lots to discover in MDN's DOM section.
Yes, you can use document.createElement
Try this:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<script>
function CreateDiv(){
var myDiv = document.createElement("DIV"); // Create a <div> node
var myTextNode = document.createTextNode("Hello World"); // Create a text node
myDiv.appendChild(myTextNode); // Append the text
//document.getElementById("myList").appendChild(node); // Append
document.body.appendChild(myDiv);
}
</script>
<input type="button" onclick="CreateDiv();" value="Create div">
</body>
</html>
You can use the native createElement() method.
var btn = document.createElement("BUTTON"); // Create a <button> element
var t = document.createTextNode("CLICK ME"); // Create a text node
btn.appendChild(t); // Append the text to <button>
document.body.appendChild(btn); // Append <button> to <body>
https://www.w3schools./jsref/met_document_createelement.asp
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div class="wrap">
<button class="btn"
onclick="document.getElementById('text').innerHTML = 'Hello'">
Click On Me</button>
<p id="text"></p>
</div>
</body>
</html>
Check this one.
本文标签: Is it possible to dynamically create divs using ONLY htmlCSS and JavascriptStack Overflow
版权声明:本文标题:Is it possible to dynamically create divs using ONLY html, css and javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742169274a2426412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论