本帖最后由 clearwater 于 2020-3-19 16:34 编辑
<!-- <div class="login-title" id="title">登陆会员 -->
<div id="title" class="login-title">登陆会员
能不能教我一下为什么120行,我把id 写在 class后面,程序就会报错, 指出162行出错,Uncaught TypeError: Cannot read property 'addEventListener' of null
我把120注释掉,把id 提到div的前面来,程序就正常运行了。
id 一定要写在类的前面吗?
[JavaScript] 纯文本查看 复制代码 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
ul,
li,
ol,
dl,
dt,
dd,
div,
p,
span,
h1,
h2,
h3,
h4,
h5,
h6,
a {
padding: 0px;
margin: 0px;
}
a {
text-decoration: none;
color: #000;
}
.login-header {
width: 100%;
height: 30px;
font-size: 24px;
text-align: center;
line-height: 30px;
}
.login {
display: none;
width: 512px;
height: 280px;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: pink;
box-shadow: 0px 0px 20px #ddd;
z-index: 999;
}
.login-title {
position: relative;
width: 100%;
height: 40px;
margin: 10px 0px 0px 0px;
text-align: center;
line-height: 40px;
font-size: 18px;
cursor: move;
}
/* 文本框标题上的关闭 */
.login-title span {
position: absolute;
top: -30px;
right: -20px;
width: 40px;
height: 40px;
border-radius: 20px;
border: #ebebeb solid 1px;
background-color: purple;
}
.login-input {
overflow: hidden;
margin: 0px 0px 20px 0px;
}
/* input.list-input之间不能有空格 */
.login-input input.list-input {
width: 350px;
height: 35px;
border: #ebebeb 1px solid;
line-height: 35px;
text-indent: 5px;
}
.login-input label {
/* label是行内元素,没有高宽,float是为了转化 */
float: left;
width: 90px;
height: 35px;
font-size: 14px;
text-align: right;
line-height: 35px;
}
.login-button {
width: 50%;
margin: 30px auto 0px auto;
background-color: skyblue;
text-align: center;
line-height: 40px;
}
.login-bg {
display: none;
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background:rgba(0,0,0, 0.3);
}
</style>
</head>
<body>
<!-- header starts -->
<div class="login-header"><a href="javascript:;" id="link">点击,弹出登录框</a></div>
<!-- header ends -->
<!-- login starts -->
<div class="login" id="login">
<!-- <div class="login-title" id="title">登陆会员 -->
<div id="title" class="login-title">登陆会员
<span><a href="javascript:void(0)" class="close-login" id="closeBtn">关闭</a></span>
</div>
<div class="login-input-content">
<!-- 两个login-input的盒子的目的是为了分为上面两行。input是行内块元素,不分两个盒子会放在一行上 -->
<div class="login-input">
<label for="">用户名:</label>
<input type="text" placeholder="请输入用户名" name="info[username]" id="username" class="list-input">
</div>
<div class="login-input">
<label for="">登录密码:</label>
<input type="password" placeholder="请输入登陆密码" name="info[password]" id="password" class="list-input" >
</div>
</div>
<div class="login-button" id="lognBtn"> <a href="javascript:void(0)"; id="long-button-submt">登陆会员</a></div>
</div>
<!-- login ends -->
<!-- mask starts -->
<div class="login-bg" id="bg"></div>
<!-- mask ends -->
<script>
// 1. 获取元素
var login = document.querySelector('.login');
var mask = document.querySelector('.login-bg');
var link = document.querySelector('#link');
var closeBtn = document.querySelector('#closeBtn');
var title = document.querySelector('#title');
// 2.点击出层这个链接,让mask 和login显示出来
link.addEventListener('click', function(){
mask.style.display = 'block';
login.style.display = 'block'
})
// 3. 点击closeBtn 就隐藏mask 和login
closeBtn.addEventListener('click', function() {
mask.style.display = 'none';
login.style.display = 'none';
})
title.addEventListener('mousedown', function(e) {
var x = e.pageX - login.offsetLeft;
var y = e.pageY - login.offsetTop;
// (2) 鼠标移动的时候,把鼠标在页面中的坐标,减去 鼠标在盒子内的坐标就是模态框的left和top值
document.addEventListener('mousemove', move)
function move(e) {
login.style.left = e.pageX - x + 'px';
login.style.top = e.pageY - y + 'px';
}
// (3) 鼠标弹起,就让鼠标移动事件移除
document.addEventListener('mouseup', function() {
document.removeEventListener('mousemove', move);
})
})
</script>
</body>
</html> |