【问题标题】:What do I do if I need a third type of quotation? [duplicate]如果我需要第三种报价,我该怎么办? [复制]
【发布时间】:2020-11-17 06:31:26
【问题描述】:

我正在使用 js 来编辑 HTML,我尝试运行的代码由引号限制,它使用的是由撇号限制的东西,而撇号又需要使用由引号限制的东西。问题是较薄的报价过早地结束了外部报价。我该怎么办? (下面的代码)请参阅this 问题了解更多上下文,或者您是否有基于此的替代解决方案

if(window.location == "https://www.hiring.example.com" || "https://example.hr/?"){
document.getElementsByTagName("a")[8].innerHTML = "<a href='/p/2066e45ed47a-commercial-real-estate-mortgage-broker/apply'><button class='button apply polygot button-right bzyButtonColor'>Apply</button><h2>Commercial Mortgage Broker — Apply Now</h2><ul class='meta'><li class='location'><i class='fa fa-wifi'></i><span class='polygot'>Remote OK</span></li><li class='type'><i class='fa fa-building'></i><span class='polygot'>Full-Time</span></li><li class='department'><i class='fa fa-building'></i><span>Multi-Family Group</span></li></ul><button class='button apply polygot button-full bzyButtonColor'>Apply</button><\a>";

document.getElementsByTagName("a")[1].innerHTML = "<a href='https://www.example.com/mfg' class='brand'><img src='https://gallery-cdn.breezy.hr/ab67d180-5ac3-43ce-8016-f80713fe5ebb/EU LOGO 3.jpg'></a>";

document.getElementsByTagName("a")[4].innerHTML = '<a href="/p/2066e45ed47a-commercial-real-estate-mortgage-broker/apply" style="background-color: rgba(0,0,0,0.2);<span class="polygot">APPLY NOW</span></a>';
}
else{
    var scrpt = document.createElement("script");
    scrpt.innerHTML = '<script>

        if(window.location == "https://www.hiring.easternunion.com/ || "https://example.hr/?" || "https://example.hr/"){

        }
        else{
        document.getElementsByTagName("a")[0].innerHTML = "<a href='https://www.example.com/mfg' class='brand'><img src='https://gallery-cdn.example.hr/ab67d180-5ac3-43ce-8016-f80713fe5ebb/EU LOGO 3.jpg'></a>";
        }
        </script>';
    document.body.appendChild(scrpt);

}

【问题讨论】:

  • 您可以使用反引号:`。此外,window.location 检查不会像那样工作,您需要在每个 || 操作员之间声明完整的测试。
  • 最好的解决方案是停止使用内联 JavaScript 进行事件处理。使用addEventListener 让它调用一个函数。

标签: javascript html


【解决方案1】:

转义你的引号,像这样:

var a = '<a onclick="alert(\'scarecrow\');">alert</a>';

理论上,您可以对任何级别的嵌套执行此操作(因此您甚至不需要两种类型的引号),但是一旦您开始需要用更多的反斜杠来转义反斜杠,它很快就会变得很痛苦。

【讨论】:

    猜你喜欢
    • 2013-11-14
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 2022-07-25
    • 2020-12-12
    • 2012-04-14
    • 1970-01-01
    相关资源
    最近更新 更多