我有以下菜單的開始顯示一個隱藏div當一個按鈕懸停,但希望能夠創建更多的按鈕和關聯的div,而不重複相同jQuery和只是改變ID名稱。使jQuery可擴展來顯示和隱藏div
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<style>
#show, #show2 {
position: absolute;
visibility: hidden;
height: 200px;
width: 100%;
background: #002061;
color: white;
z-index: 999;
transition: all .5s ease-in-out;
opacity: 0;
box-shadow: inset 0 5px 16px #001031;
}
</style>
<button id="show_div">x</button>
<button id="show_div2">y</button>
<div id="show">show</div>
<div id="show2">show 2</div>
<script>
$(document).ready(function(){
$("#show_div").mouseover(function() {
$("#show").css('visibility','visible').css('opacity','1');
});
$("#show_div").mouseout(function() {
$("#show").css('visibility','hidden').css('opacity','0');
});
$("#show").mouseover(function() {
$("#show").css('visibility','visible').css('opacity','1');
});
$("#show").mouseout(function() {
$("#show").css('visibility','hidden').css('opacity','0');
});
$("#show_div2").mouseover(function() {
$("#show2").css('visibility','visible').css('opacity','1');
});
$("#show_div2").mouseout(function() {
$("#show2").css('visibility','hidden').css('opacity','0');
});
$("#show2").mouseover(function() {
$("#show2").css('visibility','visible').css('opacity','1');
});
$("#show2").mouseout(function() {
$("#show2").css('visibility','hidden').css('opacity','0');
});
});
</script>
我已經能夠採取一些措施,使工作(見下文),但一直沒能使其在多個按鈕的工作。任何幫助將非常感激。謝謝
<script>
var arr = [ "#showMe", "#showMe2" ];
jQuery.each(arr, function(i, val) {
$("#x").mouseover(function() {
$(val).css('visibility','visible').css('opacity','1');
});
$("#x").mouseout(function() {
$(val).css('visibility','hidden').css('opacity','0');
});
$(val).mouseover(function() {
$(val).css('visibility','visible').css('opacity','1');
});
$(val).mouseout(function() {
$(val).css('visibility','hidden').css('opacity','0');
});
});
</script>
你可以使用類選擇用於此目的 –