Uncaught TypeError: Cannot read property 'style' of null at error.html:84

6 篇文章 0 订阅

要做一个判断,如果是浏览器读到是捷克语言的话,要显示捷克语文案,否则显示英语文案 代码如下

<body>
        <script>
                var JsSrc =(navigator.language || navigator.browserLanguage).toLowerCase();
                var sbtitle=document.getElementById("section");
                console.log("weichongbin JsSrc : ",JsSrc," sbtitle.style : " ,sbtitle.style);
                if(JsSrc.indexOf('cs')>=0)
                {
                // 假如浏览器语言是捷克语
                document.getElementById("section").style.display="none";//隐藏
                document.getElementById("sectionCs").style.display="block";//显示
                document.getElementById("section2").style.display="none";//隐藏
                document.getElementById("section2Cs").style.display="block";//显示
                }
                else
                {
                // 假如浏览器语言是其它语言
                document.getElementById("sectionCs").style.display="none";//隐藏
                document.getElementById("section").style.display="block";//显示
                document.getElementById("section2Cs").style.display="none";//隐藏
                document.getElementById("section2").style.display="block";//显示
                }
                </script>
<p>
    <img th:src="@{${logoUrl}}" width="36" height="36"/>
</p>
<div id="header">
</div>
<div id="section" >
    <a>No longer have access to Vivien Home</a>
</div>
<div id="sectionCs">
        <a>Už nemají přístup k Vivien Home</a>
    </div>
<div id="section2">
    <p>
        You will no longer have access to Vivien Home since she/he remove the share.
    </p>
</div>
<div id="section2Cs">
    <p>
        Již nebudete mít přístup k domácnosti Vivien Home, protože bylo zrušeno sdílení.
    </p>
</div>
</body>

但是效果图却没有隐藏,英语文案,还报错了

 

Uncaught TypeError: Cannot read property 'style' of null     at error.html:84

我就很纳闷,花了两个小时百度,Google,最后发现了,说是先执行的script,html还没渲染所以style肯定为空,将script移到html代码下面就可以了

<body>
<p>
    <img th:src="@{${logoUrl}}" width="36" height="36"/>
</p>
<div id="header">
</div>
<div id="section" >
    <a>No longer have access to Vivien Home</a>
</div>
<div id="sectionCs">
        <a>Už nemají přístup k Vivien Home</a>
    </div>
<div id="section2">
    <p>
        You will no longer have access to Vivien Home since she/he remove the share.
    </p>
</div>
<div id="section2Cs">
    <p>
        Již nebudete mít přístup k domácnosti Vivien Home, protože bylo zrušeno sdílení.
    </p>
</div>
<script>
        var JsSrc =(navigator.language || navigator.browserLanguage).toLowerCase();
        var sbtitle=document.getElementById("section");
        console.log("weichongbin JsSrc : ",JsSrc," sbtitle.style : " ,sbtitle.style);
        if(JsSrc.indexOf('cs')>=0)
        {
        // 假如浏览器语言是捷克语
        document.getElementById("section").style.display="none";//隐藏
        document.getElementById("sectionCs").style.display="block";//显示
        document.getElementById("section2").style.display="none";//隐藏
        document.getElementById("section2Cs").style.display="block";//显示
        }
        else
        {
        // 假如浏览器语言是其它语言
        document.getElementById("sectionCs").style.display="none";//隐藏
        document.getElementById("section").style.display="block";//显示
        document.getElementById("section2Cs").style.display="none";//隐藏
        document.getElementById("section2").style.display="block";//显示
        }
        </script>
</body>

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值