Best way to add DOM Element to Body with JavaScrip

2019-07-08 05:07发布

I need to put at every page this code:

document.body.innerHTML += '<div style="position:fixed; text-align:center; left:30px; widht: 200px;bottom: -12px;"> <div class="card" style="position: relative;background: #EC2D2D;border-radius: 5px;padding: 20px 0 0px 0;box-sizing: border-box;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 1px 3px rgba(0, 0, 0, 0.24);-webkit-transition: .3s ease;transition: .3s ease;"> <h1 class="title" style="position: relative;z-index: 1;border-left: 5px solid #FFFFFF;margin: 0 0 25px;padding: 10px 0 10px 10px;color: #FFFFFF;font-size: 32px;font-weight: 600;/* text-transform: uppercase; */">50% less</h1> <div class="button-container" style="margin: 0 20px;text-align: center;"> <button style="outline: 0;cursor: pointer;position: relative;display: inline-block;background: 0;color: #EC2D2D;background: #fff;width: 170px;border: 2px solid #C70A0A; padding: 10px 0;font-size: 14px;font-weight: 600;line-height: 1;text-transform: uppercase;overflow: hidden;-webkit-transition: .3s ease;transition: .3s ease;"><span>Bid and Book Now</span></button> </div> <div class="footer" style="margin: 20px 0 0;color: #FFFFFF !important;font-size: 14px;font-weight: 100;text-align: center;"> <p style="color: inherit;text-decoration: none;-webkit-transition: .3s ease;transition: .3s ease;">BEST RATES - only on website</p> <p style="font-size: 10px;">End in: 1d 2h 33m</p></div> </div> <div class="card alt" style="position: absolute;top: 40px;border: 3px solid #ED2553;right: -40px;z-index: 10;width: 80px;height: 80px;background: #FFFFFF;background-image: url(&quot;http://i.imgur.com/32T7CPH.jpg&quot;);border-radius: 100%;box-shadow: none;padding: 0;-webkit-transition: .3s ease;transition: .3s ease;"> </div> </div> ';

This code doesn't work well at every page. Also, I think its very slow and makes problem for some pages. How can I write this with appendChild or something similar?

I can't use JQuery's append because I have to use plain JavaScript code.

What would this code look like with appendChild?

2条回答
唯我独甜
2楼-- · 2019-07-08 05:34

in vanilla js you can use .appendChild which will add to the element caller the node passed as argument.

In your case (with a string as dom element/s to be insert) you could do this trick:

var el = document.createElement("div");
    el.innerHTML = "<your dom code>";

while (el.firstChild) document.body.appendChild(el.firstChild);

It's a bit rude but this way you'll create an element, filling it with your string dom and then move each child to the document.body


Here a JsFiddle as example using your code

查看更多
我命由我不由天
3楼-- · 2019-07-08 05:38

The method appendChild take a node and add this node to the element on which it was called.

var node = document.createElement("DIV");  
var textNode = document.createTextNode("My Div content");
node.appendChild(textNode);
document.body.appendChild(node); 

You can't use appendChild with a string containing the whole html.

What you can do is create a node. Set its innerHTML with the huge string. Then append the node to the body.


Code should be something similar to this:

var content = ....
var newNode = document.createElement("DIV");  
newNode.innerHTML = content;
document.body.appendChild(newNode); 
查看更多
登录 后发表回答