【发布时间】:2013-12-21 05:19:52
【问题描述】:
我遇到了障碍,不知道该去哪里找。
我有一个 200 像素 x 200 像素的 div。其中我有 4 个其他 div,它们是 65px x 65px,它们是绝对定位的,并且与外部 div 的所有 4 个角对齐。我创建了一个函数,当用户单击其中一个内部 div 时,会更新该 div 颜色并重置其他 3 个 div 背景颜色。目前,如果我单击前 2 个 div 中的任何一个,它都可以工作。如果我单击底部 2 个 div 中的任何一个,它都不起作用。
我感觉它与 z-index 和块对齐有关,但现在只是猜测。
我在下面添加了我的代码。
HTML:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="scripts1.js"></script>
<script>
$(document).ready(function(){
BuildHandler();
});
</script>
</head>
<body>
<div id="mini_container">
<div id="top_left" class="mini_boxes"></div>
<div id="top_right" class="mini_boxes"></div>
<div id="low_left" class="mini_boxes"></div>
<div id="low_right" class="mini_boxes"></div>
</div>
</body>
</html>
scripts1.js
function BoxClicked(event){
var targetBoxID = event.target.id;
switch (targetBoxID)
{
case "top_left":
$("#top_left").css("background-color","#0099FF");
$("#top_right").css("background-color","#99CCFF");
$("#low_left").css("background-color","#99CCFF");
$("#low_right").css("background-color","#99CCFF");
break;
case "top_right":
$("#top_left").css("background-color","#99CCFF");
$("#top_right").css("background-color","#0099FF");
$("#low_left").css("background-color","#99CCFF");
$("#low_right").css("background-color","#99CCFF");
case "low_left":
$("#top_left").css("background-color","#99CCFF");
$("#top_right").css("background-color","#99CCFF");
$("#low_left").css("background-color","#0099FF");
$("#low_right").css("background-color","#99CCFF");
case "low_right":
$("#top_left").css("background-color","#99CCFF");
$("#top_right").css("background-color","#99CCFF");
$("#low_left").css("background-color","#99CCFF");
$("#low_right").css("background-color","#0099FF");
}
}
function BuildHandler(){
var theBoxes = document.getElementsByClassName("mini_boxes");
for(i=0;i<theBoxes.length;i++){
theBoxes[i].addEventListener('click', BoxClicked, false);
}
}
style1.css
#mini_container{
width:200px;
height:200px;
background-color:#C4CFCE;
position:relative;
z-index:100;
}
.mini_boxes{
width:65px;
height:65px;
background-color:#99CCFF;
z-index:200;
}
#top_left{
position:absolute;
top:0px;
left:0px;
}
#top_right{
position:absolute;
top:0px;
right:0px;
}
#low_left{
position:absolute;
bottom:0px;
left:0px;
}
#low_right{
position:absolute;
bottom:0px;
right:0px;
}
任何指针将不胜感激。
【问题讨论】:
-
你可以为你的代码创建一个小提琴,以便人们调试
-
你能提供你的代码吗??
-
使用 jquery 附加点击事件,加上代码比它需要的要复杂得多。
标签: javascript jquery css