【问题标题】:Inserting an empty div after main div [duplicate]在主 div 之后插入一个空 div [重复]
【发布时间】:2018-06-12 21:21:37
【问题描述】:

我正在编辑我们的 cookie 政策横幅,但横幅是通过 JS 创建的,所以我真的可以添加任何 HTML,除非我使用 JS 添加它。

基本上生成的内容如下

<div id="cookieChoiceInfo">
<span>This is the text explaining the cookie policy</span>
<span><a href="#">Okay</a></span>
</div>

我想做的是在主 div 之后包含另一个 div - 所以类似于内部 div。

这是生成外部 div 的 JS 的主要部分

var cookieChoices = (function() {
    var cookieName = 'displayCookieConsent';
    var cookieConsentId = 'cookieChoiceInfo';
    var dismissLinkId = 'cookieChoiceDismiss';

function _createHeaderElement(cookieText, dismissText, linkText, linkHref) {

      var butterBarStyles = 'position:fixed;background-color:#eee;' +
            'margin:0; top:0;padding:15px;z-index:1000;text-align:center;';

      var cookieConsentElement = document.createElement('div');
      cookieConsentElement.id = cookieConsentId;
      cookieConsentElement.style.cssText = butterBarStyles;
      cookieConsentElement.appendChild(_createConsentText(cookieText));

      if (!!linkText && !!linkHref) {
        cookieConsentElement.appendChild(_createInformationLink(linkText, linkHref));
      }
      cookieConsentElement.appendChild(_createDismissLink(dismissText));
      return cookieConsentElement;
    }

任何帮助将不胜感激。

提前致谢。

【问题讨论】:

    标签: javascript html css


    【解决方案1】:

    我假设您希望 div 作为 cookieChoiceInfo 的子级:

    var innerDiv = document.createElement('div');
    // innerDiv.id = ...;
    // innerDiv.style.cssText = ...;
    cookieConsentElement.appendChild(innerDiv);
    

    如果你真的想要它div之后:

    cookieConsentElement.parentNode.insertBefore(innerDiv, cookieConsentElement.nextSibling);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-15
      • 2022-09-25
      • 1970-01-01
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多