我有两个横幅,需要显示当一个div存在于我的HTML,我已经做了这个脚本,它的工作完美。问题是,我正在使用GTM,它给出了这个错误:"该语言功能仅支持ECMASCRIPT_2015模式或更好的模式:块作用域函数声明。"如何将函数转换为GTM接受的代码?
var element = document.getElementById('chamaBannerVB')
var elementZC = document.getElementById('chamaBannerZC')
if (element !== null) {
function appendHtmlVB() {
var divVB = document.getElementById("chamaBannerVB");
divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbookVB").appendChild(styleSheet);
}
window.onload = appendHtmlVB;
var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;" +"}}"
} else {
function appendHtml() {
var div = document.getElementById("chamaBannerZC");
div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbook").appendChild(styleSheet);
}
window.onload = appendHtml;
var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;" +"}}"
}
<!-- <div id="chamaBannerVB" style="text-align: center; display: flex; justify-content: center; margin: 16px 0; "></div> -->
<div id="chamaBannerZC" style="text-align: center; display: flex; justify-content: center; margin: 16px 0; "></div>
使用任意一个全局函数(在top作用域声明)
function appendHtmlVB() {
var divVB = document.getElementById("chamaBannerVB");
divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbookVB").appendChild(styleSheet);
}
function appendHtml() {
var div = document.getElementById("chamaBannerZC");
div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbook").appendChild(styleSheet);
}
var element = document.getElementById('chamaBannerVB')
var elementZC = document.getElementById('chamaBannerZC')
if (element !== null) {
window.onload = appendHtmlVB;
var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;" +"}}"
} else {
window.onload = appendHtml;
var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;" +"}}"
}
或者根本不使用函数声明,直接将函数表达式赋值给事件处理程序属性:
var element = document.getElementById('chamaBannerVB')
var elementZC = document.getElementById('chamaBannerZC')
if (element !== null) {
window.onload = function appendHtmlVB() {
var divVB = document.getElementById("chamaBannerVB");
divVB.innerHTML += '<a href="https://vidabalanceada.com.br/facaparte?mktcode=IDSTCS" target="_blank"><img class="bannerEbookVB" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_vb_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbookVB").appendChild(styleSheet);
};
var styles = "@media (min-width: 768px) {" + ".bannerEbookVB {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_vb_LS_03-2023.png); max-width: 800px!important;" +"}}"
} else {
window.onload = function appendHtml() {
var div = document.getElementById("chamaBannerZC");
div.innerHTML += '<a href="https://zenconecta.com.br/facaparte?mktcode=IZSTCS" target="_blank"><img class="bannerEbook" src="https://img.selecoesbrasil.com.br/banner_ebook/mobile_banner_ebook_zc_LS_03-2023.png" alt="Banner Mobile" style="width: 100%; max-width: 480px;"></a>';
var styleSheet = document.createElement("style")
styleSheet.innerText = styles
document.querySelector(".bannerEbook").appendChild(styleSheet);
};
var styles = "@media (min-width: 768px) {" + ".bannerEbook {" + "content: url(https://img.selecoesbrasil.com.br/banner_ebook/banner_ebook_zc_LS_03-2023.png); max-width: 800px!important;" +"}}"
}