【发布时间】: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