此语言功能仅支持 ECMASCRIPT_2015 模式或更高版本:块作用域函数声明

问题描述 投票:0回答:1

当我的 HTML 中存在一个 div 时,我有两个横幅需要显示,我已经制作了这个脚本并且它完美运行。问题是我正在使用 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>

javascript html ecmascript-6 google-tag-manager ecmascript-5
1个回答
1
投票

使用全局函数(在顶级范围内声明)

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;" +"}}"
}
© www.soinside.com 2019 - 2024. All rights reserved.