Uncaught TypeError: Cannot read properties of null (reading 'style')
<script src="js/1.login.js"></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(){}框起来