麻烦老师 为什么我的点击事件没有反应?
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>全屏的云南旅游相册</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/script.js"></script>
<script src="http://cdn.bootcss.com/jquery/1.12.4/jquery.js"></script>
<style type="text/css">
* {
margin: 0;
padding: 0;
border: none;
}
html,
body {
overflow: hidden;
height: 100%;
background: #93b3c6;
}
span {
display: block;
width: 16px;
height: 16px;
margin: 30px auto 40px;
border-radius: 50%;
background: #fff;
}
nav {
position: relative;
display: flex;
width: 78.125vw;
margin: 0 auto 45px;
justify-content: space-between;
}
nav:before {
position: absolute;
top: 20px;
display: block;
width: 100%;
height: 10px;
content: "";
background: #fff;
}
nav > a {
font-size: 20px;
position: relative;
padding: 10px;
text-decoration: none;
color: #255d7e;
border: 2px solid #5395b4;
background: #fff;
}
div {
position: relative;
overflow: hidden;
width: 78.125vw;
height: 75vh;
margin: 0 auto;
background: #fff;
box-shadow: 0 0 30px 0 rgba(8, 1, 3, 0.3);
}
div > img {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
width: 98%;
height: 96%;
margin: auto;
}
</style>
</head>
<body>
<span></span>
<nav>
<a href="#">泸沽湖</a>
<a href="#">丽江古城</a>
<a href="#">茶马古道</a>
<a href="#">就这家·云逸客栈</a>
<a href="#">西双版纳</a>
<a href="#">云南红酒庄</a>
<a href="#">轿子雪山</a>
<a href="#">普者黑</a>
<a href="#">海埂大坝</a>
<a href="#">玉龙湾</a>
<a href="#">昆明郊野公园</a>
<a href="#">欧洲风琴小镇</a>
</nav>
<div>
<img src="images/1.jpg">
<img src="images/2.jpg">
<img src="images/3.jpg">
<img src="images/4.jpg">
<img src="images/5.jpg">
<img src="images/6.jpg">
<img src="images/7.jpg">
<img src="images/8.jpg">
<img src="images/9.jpg">
<img src="images/10.jpg">
<img src="images/11.jpg">
<img src="images/12.jpg">
</div>
</body>
<script type="text/javascript">
$(document).ready(function() {
var index = 0;
function change(i) {
$('img').eq(i).css({
'opacity': '1',
}).siblings().css({
'opacity': '0',
})
}
/* $('a').click(function() {
index = $(this).index();
change(index);
});
$(document).keydown(function(event) {
//左37 右39
if (event.keyCode == 37) {
index = (index > 0) ? --index : $('a').length - 1;
} else if (event.keyCode == 39) {
index = (index < $('a').length - 1) ? ++index : 0;
} else {
return false;
}
change(index);
}) */
$("a").add(document).on({
click: function(e) {
e.preventDefault();
index = $(this).index();
change(index);
},
keydown: function(event) {
if (event.keyCode == 37) {
index = (index > 0) ? --index : $('a').length - 1;
} else if (event.keyCode == 39) {
index = (index < $('a').length - 1) ? ++index : 0;
} else {
return true;
}
change(index);
}
})
});
</script>
</html>
正在回答
同学你好,理解的冒泡机制没有问题。但是老师说了a元素先执行点击事件,然后再冒泡到document上执行点击事件
并没有说a元素不执行点击事件哦,建议再仔细看一下回复。
祝学习愉快!
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星