How can I create and style a div using JavaScript?

后端 未结 9 2268
野的像风
野的像风 2020-11-27 10:27

How can I use JavaScript to create and style (and append to the page) a div, with content? I know it\'s possible, but how?

相关标签:
9条回答
  • 2020-11-27 10:48

    var div = document.createElement("div");
    div.style.width = "100px";
    div.style.height = "100px";
    div.style.background = "red";
    div.style.color = "white";
    div.innerHTML = "Hello";
    
    document.getElementById("main").appendChild(div);
    <body>
    <div id="main"></div>
    </body>

    var div = document.createElement("div");
    div.style.width = "100px";
    div.style.height = "100px";
    div.style.background = "red";
    div.style.color = "white";
    div.innerHTML = "Hello";
    
    document.getElementById("main").appendChild(div);
    OR
    document.body.appendChild(div);
    

    Use parent reference instead of document.body.

    0 讨论(0)
  • 2020-11-27 10:49

    You can create like this

    board.style.cssText = "position:fixed;height:100px;width:100px;background:#ffffd;"
    
    document.getElementById("main").appendChild(board);
    

    Complete Runnable Snippet:

    var board;
    board= document.createElement("div");
    board.id = "mainBoard";
    board.style.cssText = "position:fixed;height:100px;width:100px;background:#ffffd;"
        
    document.getElementById("main").appendChild(board);
    <body>
    <div id="main"></div>
    </body>

    0 讨论(0)
  • 2020-11-27 10:50

    this solution uses the jquery library

    $('#elementId').append("<div class='classname'>content</div>");
    
    0 讨论(0)
  • 2020-11-27 10:51

    Here's one solution that I'd use:

    var div = '<div id="yourId" class="yourClass" yourAttribute="yourAttributeValue">blah</div>';
    

    If you wanted the attribute and/or attribute values to be based on variables:

    var id = "hello";
    var classAttr = "class";
    var div = '<div id='+id+' '+classAttr+'="world" >Blah</div>';
    

    Then, to append to the body:

    document.getElementsByTagName("body").innerHTML = div;
    

    Easy as pie.

    0 讨论(0)
  • 2020-11-27 10:54

    While other answers here work, I notice you asked for a div with content. So here's my version with extra content. JSFiddle link at the bottom.

    JavaScript (with comments):

    // Creating a div element
    var divElement = document.createElement("Div");
    divElement.id = "divID";
    
    // Styling it
    divElement.style.textAlign = "center";
    divElement.style.fontWeight = "bold";
    divElement.style.fontSize = "smaller";
    divElement.style.paddingTop = "15px";
    
    // Adding a paragraph to it
    var paragraph = document.createElement("P");
    var text = document.createTextNode("Another paragraph, yay! This one will be styled different from the rest since we styled the DIV we specifically created.");
    paragraph.appendChild(text);
    divElement.appendChild(paragraph);
    
    // Adding a button, cause why not!
    var button = document.createElement("Button");
    var textForButton = document.createTextNode("Release the alert");
    button.appendChild(textForButton);
    button.addEventListener("click", function(){
        alert("Hi!");
    });
    divElement.appendChild(button);
    
    // Appending the div element to body
    document.getElementsByTagName("body")[0].appendChild(divElement);
    

    HTML:

    <body>
      <h1>Title</h1>
      <p>This is a paragraph. Well, kind of.</p>
    </body>
    

    CSS:

    h1 { color: #333333; font-family: 'Bitter', serif; font-size: 50px; font-weight: normal; line-height: 54px; margin: 0 0 54px; }
    
    p { color: #333333; font-family: Georgia, serif; font-size: 18px; line-height: 28px; margin: 0 0 28px; }
    

    Note: CSS lines borrowed from Ratal Tomal

    JSFiddle: https://jsfiddle.net/Rani_Kheir/erL7aowz/

    0 讨论(0)
  • 2020-11-27 10:57

    This will be inside a function or script tag with custom CSS with classname as Custom

     var board = document.createElement('div');
     board.className = "Custom";
     board.innerHTML = "your data";
     console.log(count);
     document.getElementById('notification').appendChild(board);
    
    0 讨论(0)
提交回复
热议问题