Uncaught TypeError: Cannot read properties of null (readingstyle‘)

    &lt;script src="js/1.login.js"&gt;</script>
</head>
var olFont=document.querySelector('.olFont');//Cannot read properties of null (reading 'style')
function showOl(){
    // var olFont=document.querySelector('.olFont');
    if(olFont.style.display=='none' || olFont.style.display==''){
        olFont.style.display='block';
    }else{
        olFont.style.display='none';
    }
}
document.addEventListener('click',function(){
    // var olFont=document.querySelector('.olFont');
    olFont.style.display='none';
});

2:报错原因
文档加载过程是自上向下加载使用命名变量、会报错

3;解决办法
1、将Javascript代码标签中放入<body>中
2、 window.onload = function(){}框起来

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注