2011-01-06 54 views
1

我有一個切換可見性div,它在表格外工作,但裏面沒有。任何幫助?在桌子外面切換不在裏面

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-1.4.4.js"></script> 
<style type="text/css"> 
#hidden{ 
    background-color:gray; 
    width:120px; 
    text-decoration:none; 
    font-size:x-large; 
    top:120px; 
    color:black; 
    display:none; 
    border:thin; 
    border-bottom-color:gray; 
} 

.style1 { 
    border-style: solid; 
    border-width: 1px; 
} 
.style2 { 
    border-width: 1px; 
} 

</style> 
</head> 

<body> 

    <p id="button"><a href="#">Toggle</a></p> 

<div id="hidden">woot</div> 
<script> 

$("#button").hover(function() { 
$("#hidden").toggle(); 
}); 
</script> 

<table style="width: 35%" cellspacing="1" class="style1"> 
    <tr> 
     <td class="style2" style="width: 164px"><p id="button"><a href="#">Toggle</a></p> 
</td> 
     <td class="style2"><div id="hidden">woot</div></td> 
    </tr> 
    <tr> 
     <td class="style2" style="width: 164px">&nbsp;</td> 
     <td class="style2">&nbsp;</td> 
    </tr> 
</table> 

</body> 
</html> 

回答

1

你不能在一個頁面中兩次使用同一ID,它會在失敗一些方式......通常只連接到第一個實例(如你所看到的)。

相反,使用類,如果多個元素,像這樣:

<p class="button"><a href="#">Toggle</a></p> 

而且從$("#button")您的選擇更改爲$(".button")

+0

感謝您的答覆! – curious 2011-01-06 19:32:56