gpt4 book ai didi

jquery - 如何为多个 div id 绑定(bind)单个功能

转载 作者:行者123 更新时间:2023-11-28 18:33:14 25 4
gpt4 key购买 nike

我想对大量列表使用工具提示

我为每个 div 写了一个 jquery,但我不知道为多个 div 元素绑定(bind)一个功能

这是链接 http://masterssoftware.in/tool_tip2.html在上面提到的代码下面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
$("#a1_down").hover(function(){
$("#tip1_down").toggle();
});

$("#a2_down").hover(function(){
$("#tip2_down").toggle();
});

$("#a3_down").hover(function(){
$("#tip3_down").toggle();
});


});

</script>
<style>
body{ background:#690;}
.tip{ width:500px; height:100px; padding:10px; background:#CFF; display:none; overflow:hidden; border-radius:5px 5px 5px 5px; z-index:200; position:absolute;}
.tip h4{ display:block; font-size:18px; text-transform:uppercase;}
.tip p{ font-size:14px;}
</style>
</head>

<body>
<ul>
<li><a id="a1_down" href="#">aaaaaaaaaaa</a></li>
<div id="tip1_down" class="tip"> <h4>content of aaaaaaaa aaaaaaaaaaaaaa </h4>
<p>content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa</p>
</div>

<li><a id="a2_down" href="#">aaaaaaaaaaa</a></li>
<div id="tip2_down" class="tip"> <h4>content of aaaaaaaa aaaaaaaaaaaaaa </h4>
<p>content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa</p>
</div>
<li><a id="a3_down" href="#">aaaaaaaaaaa</a></li>
<div id="tip3_down" class="tip"> <h4>content of aaaaaaaa aaaaaaaaaaaaaa </h4>
<p>content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa content of aaaaaaaa aaaaaaaaaaaaaa</p>
</div>
</ul>
</body>
</html>

最佳答案

除了使用 ID,您还可以使用 parentnext 方法:

$(document).ready(function(){
$("ul li a").hover(function(){
$(this).parent().next().toggle()
});
});

或者使用类:

$(document).ready(function(){
$("ul a.classname").hover(function(){
$(this).parent().next('.tip').toggle()
});
});

关于jquery - 如何为多个 div id 绑定(bind)单个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13680327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com