【发布时间】:2016-06-09 21:08:50
【问题描述】:
我需要在每个页面上放置这段代码:
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("http://i.imgur.com/32T7CPH.jpg");border-radius: 100%;box-shadow: none;padding: 0;-webkit-transition: .3s ease;transition: .3s ease;"> </div> </div> ';
此代码并非在每个页面都运行良好。另外,我认为它非常慢并且对某些页面造成了问题。我怎样才能用appendChild 或类似的东西写这个?
我不能使用 JQuery 的 append,因为我必须使用纯 JavaScript 代码。
如果使用appendChild,这段代码会是什么样子?
【问题讨论】:
-
您在“某些页面”遇到了什么问题?
-
我的控制台日志块 - 并重写所有 body dom 元素...
-
是的,因为您正在为整个 HTML 设置新内容。你的代码说:取body的innerHTML(即一个字符串)添加一个字符串(你在顶点之间的字符串)并将这个新字符串设置为innerHTML,你没有附加它。
-
那么我该如何解决这个问题并使用 appendChild ?
-
这个问题要么过于宽泛,要么基于观点,要么需要讨论,因此与 Stack Overflow 无关。如果您有具体的、可回答的编程问题,请提供完整的详细信息。
标签: javascript jquery css innerhtml appendchild