发布网友 发布时间:2022-04-22 10:24
共3个回答
热心网友 时间:2022-04-25 02:03
//根据id查找元素
function getEl(id){
return document.getElementById(id);
}
window.onload = function(){
[
['d10' , 'd11'] , //这里是要做处理的元素id
['d12' , 'd13'] , //分别用数组包起来
['d14' , 'd15']
].map(function(ids){
return [getEl(ids[0]) , getEl(ids[1])];//这里先查找元素
}).forEach(function(group){//然后遍历分别做处理
var el_1 = group[0],
el_2 = group[1];
el_1.onmouseover=function(){
el_2.style.display="block";
};
el_1.onmouseout=function(){
el_2.style.display="none";
}
el_2.onmouseover=function(){
el_2.style.display="block";
};
el_2.onmouseout=function(){
el_2.style.display="none";
}
});
};
热心网友 时间:2022-04-25 03:21
<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
</style>
<script type="text/javascript">
window.onload = function ()
{
for ( var i = 10; i < 16; i += 2)
{
var dx = document.getElementById ("d" + i), dy = document.getElementById ("d" + (i + 1));
dx.onmouseover = function ()
{
dy.style.display = "block";
};
dx.onmouseout = function ()
{
dy.style.display = "none";
}
dy.onmouseover = function ()
{
dy.style.display = "block";
};
dy.onmouseout = function ()
{
dy.style.display = "none";
}
}
}
</script>
</head>
<body>
</body>
</html>
追问不循环!
追答<!DOCTYPE HTML>
<html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
div {
border: 1px solid black;
}
</style>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript">
window.onload = function ()
{
for ( var i = 10; i < 16; i += 2)
{
var dx = document.getElementById ("d" + i), dy = document.getElementById ("d" + (i + 1));
dx.index = i;
dx.onmouseover = function ()
{
document.getElementById ("d" + (this.index + 1)).style.display = "block";
};
dx.onmouseout = function ()
{
document.getElementById ("d" + (this.index + 1)).style.display = "none";
}
dy.onmouseover = function ()
{
this.style.display = "block";
};
dy.onmouseout = function ()
{
this.style.display = "none";
}
}
}
</script>
</head>
<body>
<div id="d10">10</div>
<div id="d11">11</div>
<div id="d12">12</div>
<div id="d13">13</div>
<div id="d14">14</div>
<div id="d15">15</div>
</body>
</html>
热心网友 时间:2022-04-25 04:56
用jQuery 写呗,循环都不用 $("#d10,#d11,#d12,...")或者使用其他选择器找到这些元素 在绑定具体的事件