简体   繁体   English

使用JavaScript将DOM元素添加到主体的最佳方法

[英]Best way to add DOM Element to Body with JavaScript

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? 我如何用appendChild或类似的东西写这个?

I can't use JQuery's append because I have to use plain JavaScript code. 我不能使用JQuery的append因为我必须使用普通的JavaScript代码。

What would this code look like with appendChild ? 这段代码与appendChild会是什么样子?

The method appendChild take a node and add this node to the element on which it was called. appendChild方法采用一个节点 ,并将该节点添加到调用它的元素中。

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. 您不能将appendChild与包含整个html的字符串一起使用。

What you can do is create a node. 您可以做的就是创建一个节点。 Set its innerHTML with the huge string. 使用巨大的字符串设置其innerHTML 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); 

in vanilla js you can use .appendChild which will add to the element caller the node passed as argument. 在普通js中,您可以使用.appendChild ,它将作为参数传递的节点添加到元素调用者中。

In your case (with a string as dom element/s to be insert) you could do this trick: 在您的情况下(将一个字符串作为dom元素插入),您可以执行以下操作:

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 这有点不礼貌,但是通过这种方式,您将创建一个元素,并用字符串dom填充它,然后将每个孩子移到文档中。


Here a JsFiddle as example using your code 这里以使用代码的JsFiddle为例

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

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