【问题标题】:Append DIV with link style使用链接样式附加 DIV
【发布时间】:2018-09-16 10:52:58
【问题描述】:

我有数百个 HTML 页面,我想为它们添加一个带有链接和样式的新 div,但编辑每个页面需要大量时间。

所有页面都有一个通用的 JavaScript 文件。

如何将下面的 div 附加或添加到所有页面?

<div  style="background-color:#561D1B;" id="testid">
    <marquee style="background-color:#561D1B;height:19px;"><script src="https://testurl.com/test" type="text/javascript" ></script></marquee>
    </div>

avoid-fout 是一个类,我尝试添加以下代码,但它不起作用。

$(".avoid-fout").append("<div  style="background-color:#561D1B;" id="testid">
    <marquee style="background-color:#561D1B;height:19px;"><script src="https://testurl.com/test" type="text/javascript" ></script></marquee>
    </div>");

【问题讨论】:

  • 我试过了,但它不工作......看我的问题
  • 你的脚本src真的是一个有效的js吗???

标签: javascript jquery html css


【解决方案1】:

追加时,一定要注意单引号和双引号的使用,如下图

$(".avoid-fout").append('<div style="background-color:#561D1B;" id="testid">
<marquee style="background-color:#561D1B;height:19px;"><script src="https://testurl.com/test" type="text/javascript" ></script></marquee>
</div>');

先用单引号,然后里面的一切都应该用双引号,反之亦然。

【讨论】:

  • 它不工作的控制台说 Uncaught SyntaxError: Invalid or unexpected token
【解决方案2】:

试试这个

$(".avoid-fout").append(`<div style="background-color:#561D1B;" id="testid"> <marquee style="background-color:#561D1B;height:19px;"><script src="https://testurl.com/test" type="text/javascript" ></script></marquee>
</div>`);

还要确保你的脚本 src 是一个有效的 JS

【讨论】:

    【解决方案3】:

    您的.append 调用将不起作用,因为您正在附加一个带双引号的字符串,并且还在 html 中使用双引号。最简单的解决方法是为您的 html 字符串使用单引号:

    $(".avoid-fout").append('<div  style="background-color:#561D1B;" id="testid">
    <marquee style="background-color:#561D1B;height:19px;"><script src="https://testurl.com/test" type="text/javascript" ></script></marquee>
    </div>');`
    

    【讨论】:

    • 它不工作的控制台说 Uncaught SyntaxError: Invalid or unexpected token
    • 这是因为字符串跨越多行,JS中的单引号和双引号字符串不能包含新行(unless你用反斜杠转义它们,但这可能容易出错而不是很多人都知道。)。我要么将它加入一行,要么如果在您的目标环境中可行,请使用 template literal 而不是单引号或双引号字符串。
    【解决方案4】:

    如果您不想使用 Jquery 或避免插入 HTML 字符串,这可能会有所帮助。它使用香草 JS dom API。您可以修改方法appendToElem,以防您需要添加任何其他内容

            function appendToElem(elem) {
                if (!elem) {
                    console.error(elem)
                }
                var script = document.createElement('script')
                script.setAttribute("src", "https://testurl.com/test")
                script.setAttribute("type", "text/javascript")
    
                var marquee = document.createElement('marquee')
                marquee.style.backgroundColor = "#561D1B"
                marquee.style.height = "19px"
                marquee.appendChild(script)
    
                var div = document.createElement("div")
                div.id = "testid"
                div.style.backgroundColor = "#561D1B"
                div.appendChild(marquee)
    
                elem.appendChild(div)
            }
            appendToElem(document.getElementsByClassName('avoid-fout')[1])
        <div class="avoid-fout">
            <h2>Some Random title</h2>
            <p>Some Text</p>
        </div>
        <div class="avoid-fout">
            <h2>Add At the end of this container</h2>
            <p>
                Add the asked div, with marquee and script below this. 
            </p>
        </div>

    【讨论】:

      【解决方案5】:

      有几个问题。您正在尝试在双引号字符串中使用双引号,如果没有 escaping 双引号,您将无法做到这一点。

      "This is "not" a valid string";
      "This \"is\" a valid string";
      

      我通常使用的一个简单解决方案是只使用单引号来封装 HTML,并且只在我的 HTML 中使用双引号:

      '<div id="double-quotes-work-here">Just can\'t use unescaped single-quotes</div>';
      

      另一个问题是您尝试使用跨越多行的字符串。 JS 中的单引号和双引号字符串一般不能包含任何换行符。在这种情况下,您只是插入 HTML 而新行实际上并不重要,最简单的做法是删除新行并将所有 HTML 包含在一行中:

      $(".avoid-fout").append('&lt;div  style="background-color:#561D1B;" id="testid"&gt; &lt;marquee style="background-color:#561D1B;height:19px;"&gt;Foo&lt;/marquee&gt; &lt;/div&gt;');
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="avoid-fout"></div>

      如果你想保持多行,有a few options

      您可以使用多个串联的字符串:

      $(".avoid-fout").append('<div  style="background-color:#561D1B;" id="testid">' +
      '<marquee style="background-color:#561D1B;height:19px;">Foo</marquee>' +
      '</div>');
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="avoid-fout"></div>

      或者您可以使用 \ 转义换行符:

      $(".avoid-fout").append('<div  style="background-color:#561D1B;" id="testid">\
      <marquee style="background-color:#561D1B;height:19px;">Foo</marquee>\
      </div>');
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="avoid-fout"></div>

      这种方式应该避免。比较容易出错,如果\后面有空格,会导致语法错误;一个真的很难注意到,因为空白是不可见的。例如:

      'this is a valid\
      multi-line string';
      
      'this is not a valid\ 
      multi-line string';
      

      看到区别了吗?我也没有。

      如果您的目标浏览器都支持它(most modern browser do),我会使用template literal

      $(".avoid-fout").append(`<div  style="background-color:#561D1B;" id="testid">
      <marquee style="background-color:#561D1B;height:19px;">Foo</marquee>
      </div>`);
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div class="avoid-fout"></div>

      模板文字允许您使用多行字符串而不进行任何转义,您可以在其中使用单引号和双引号而不进行转义。它们还允许您执行其他简洁的操作,例如 expression interpolation 和使用 tagged templates

      学习在浏览器中使用 developer console 以及诸如 jsHint 之类的 linter 有助于发现此类错误。

      【讨论】:

        猜你喜欢
        • 2013-06-08
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-09
        • 1970-01-01
        • 1970-01-01
        • 2017-07-22
        相关资源
        最近更新 更多