使用JavaScript将DOM元素添加到Body的最佳方法 [英] Best way to add DOM Element to Body with JavaScript

查看:454
本文介绍了使用JavaScript将DOM元素添加到Body的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每个页面放置此代码:

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> ';

此代码在每个页面都不能正常工作。此外,我认为它非常慢并且对某些页面造成问题。如何用 appendChild 或类似的东西来写这个?

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?

我不能使用JQuery的 append 因为我必须使用普通的JavaScript代码。

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

使用 appendChild

推荐答案

方法 appendChild take节点并将此节点添加到调用它的元素。

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); 

你不能使用 appendChild 包含整个html的字符串。

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

您可以做的是创建一个节点。使用巨大的字符串设置 innerHTML 。然后将节点附加到正文。

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

代码应与此类似:

var content = ....
var newNode = document.createElement("DIV");  
newNode.innerHTML = content;
document.body.appendChild(newNode); 

这篇关于使用JavaScript将DOM元素添加到Body的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆