2013-04-08 61 views
-2

我在我的asp頁面上做了一個鼠標懸停功能。這是我的代碼:鼠標懸停事件無法運行

<!DOCTYPE html> 
<html> 
<head runat="server"> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"> 
</script> 
    <script> 
     $(document).ready(function(){ 
      $(".Menu1").mouseover(
      function() { $("td_1").show(); }, 
      function() { $("td_1").hide(); } 
     ); 
      $(".Menu2").mouseover(
      function() { $("td_2").show(); }, 
      function() { $("td_2").hide(); } 
     ); 
      $(".Menu3").mouseover(
      function() { $("td_3").show(); }, 
      function() { $("td_3").hide(); } 
     ); 
      $(".Menu4").mouseover(
      function() { $("td_4").show(); }, 
      function() { $("td_4").hide(); } 
     ); 
      $(".Menu5").mouseover(
      function() { $("td_5").show(); }, 
      function() { $("td_5").hide(); } 
     ); 
      $(".Menu6").mouseover(
      function() { $("td_6").show(); }, 
      function() { $("td_6").hide(); } 
     ); 
      $(".Menu7").mouseover(
      function() { $("td_7").show(); }, 
      function() { $("td_7").hide(); } 
     ); 
      $(".Menu8").mouseover(
      function() { $("td_8").show(); }, 
      function() { $("td_8").hide(); } 
     ); 
     }) 
</script> 
</head> 
<body> 
    <form runat="server"> 
<table width="963" height="70" border="0" cellpadding="0" cellspacing="0"> 
     <tr> 
      <td style="width:99px"> 
       <asp:Menu ID="Menu1" BackColor="Purple" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false">     
        <Items> 
          <asp:MenuItem NavigateUrl="index.aspx" Text="Hello1">        
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 
      </td> 
      <td style="width:112px"> 
       <asp:Menu ID="Menu2" BackColor="Yellow" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 
        <Items> 
          <asp:MenuItem NavigateUrl="open-live-account.aspx" Text="Hello2"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 
      </td> 
      <td style="width:117px"> 
       <asp:Menu ID="Menu3" BackColor="Red" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 
        <Items> 
          <asp:MenuItem NavigateUrl="education-center.aspx" Text="Hello3"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 

      </td> 
      <td style="width:121px"> 
       <asp:Menu ID="Menu4" BackColor="Black" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 
        <Items> 
          <asp:MenuItem NavigateUrl="investment-news.aspx" Text="Hello4"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 

      </td> 
       <td> 

      <asp:Menu ID="Menu5" BackColor="Blue" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 

        <Items> 
          <asp:MenuItem NavigateUrl="affiliate-program.aspx" Text="Hello5"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 

      </td> 
      <td> 
      <asp:Menu ID="Menu6" BackColor="Green" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 

        <Items> 
          <asp:MenuItem NavigateUrl="support.aspx" Text="Hello6"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 

      </td> 
      <td style="width:118px"> 
      <asp:Menu ID="Menu7" BackColor="Pink" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 

        <Items> 
          <asp:MenuItem NavigateUrl="about-us.aspx" Text="Hello7"> 
          </asp:MenuItem>       
        </Items> 
        </asp:Menu> 
      </td> 
      <td style="width:140px"> 
      <asp:Menu ID="Menu8" BackColor="Orange" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false"> 
       <Items> 
          <asp:MenuItem NavigateUrl="contact-us.aspx" Text="Hello8"> 
       </asp:MenuItem>       
        </Items> 
        </asp:Menu> 
      </td> 
     </tr> 
    </table> 
     <table> 
    <tr> 
     <td><table width="963" height="15" border="0" cellspacing="0" cellpadding="0" style="display:none"> 
     <tr> 
      <td id="td_1" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"> 
      <a href="open-live-account.aspx" class="style101">Open Live Account</a>&nbsp;&nbsp; | &nbsp;&nbsp; 
      <a href="/File/nzf4setup.exe" class="style101">Download</a>&nbsp;&nbsp; | &nbsp;&nbsp; 
      <a href="/deposit.aspx" class="style101">Deposit</a>&nbsp;&nbsp; | &nbsp;&nbsp; 
      <a href="/withdraw.aspx" class="style101">Withdrawal</a> 
       </div> 
      </td> 
     </tr> 
      <tr> 
      <td id="td_2" colspan="8" bgcolor="#CF0D0B" height="25"> 
           <div align="center" class="style101"> 
            <a href="open-live-account.aspx" class="style101">Open Live Account</a>&nbsp;&nbsp; 
            | &nbsp;&nbsp;<a href="deposit.html" class="style101">Deposit</a><a href="nzf-seminar.html" 
             class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="withdraw.html" class="style101">Withdraw</a><a 
              href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="trading-platform.html" 
               class="style101">Trading Platform</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; 
            | &nbsp;&nbsp;<a href="market-hours.html" class="style101">Market Hours</a><a href="nzf-seminar.html" 
             class="style101"></a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; 
            | &nbsp;&nbsp;<a href="trading-services.html" class="style101">Trading Services</a><a 
             href="nzf-seminar.html" class="style101"></a></div> 
      </td> 
       </tr> 
      <tr> 
      <td id="td_3" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="education-center.html" class="style101">Education Center</a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="risk-assessment.html" class="style101">Risk Assessment</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="risk-warning.html" class="style101">Risk Warning</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="money-management-responsible-trading.html" class="style101">Money Management &amp; Responsiblity Trading</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
      </tr> 
      <tr> 
      <td id="td_4" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="investment-news.html" class="style101">Investment news</a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="economic-calendar.html" class="style101">Economic Calendar</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
       </tr> 
      <tr> 
      <td id="td_5" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="affiliate-program.html" class="style101">Affiliate Program</a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="white-label.html" class="style101">White Label</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
      </tr> 
      <tr> 
      <td id="td_6" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="contact-us.aspx" class="style101">Contact Us</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="nzf-faq.html" class="style101">FAQ</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="site-map.html" class="style101">SiteMap</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
       </tr> 
      <tr> 
      <td id="td_7" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="about-us.html" class="style101">About Us</a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="nzf-vision.html" class="style101">NZF Vision</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="careers.html" class="style101">Careers</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
       </tr> 
      <tr> 
      <td id="td_8" colspan="8" bgcolor="#CF0D0B" height="25"><div align="center" class="style101"><a href="contact-us.aspx" class="style101">Contact Us</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="nzf-faq.html" class="style101">FAQ</a><a href="nzf-seminar.html" class="style101"></a>&nbsp;&nbsp; | &nbsp;&nbsp;<a href="site-map.html" class="style101">SiteMap</a><a href="nzf-seminar.html" class="style101"></a></div></td> 
       </tr> 
      </table> 
      </tr> 
    </table> 
     </form> 
</body> 
</html> 

不知何故,當我嘗試mouseover時,它不響應我。我可以知道我做錯了什麼嗎?我嘗試使用調試工具檢查是否有任何錯誤,但到目前爲止工具上沒有顯示任何錯誤。

回答

1

在您的jQuery中,您按類別(例如$(".Menu1").mouseover)定位,但在您的ASP內部,您已將其替換爲ID(Menu ID="Menu1")。

  • 首先,嘗試將它們都更改爲相同的類型(兩個類或兩個ID)。
  • 其次,你是在你的jQuery目標td_1等,但沒有使用ID /類選擇器,所以它永遠不會工作。

例如,你的第一部分也類似於以下內容:

的jQuery:

$("#Menu1").hover(
    function() { $("#td_1").show(); }, 
    function() { $("#td_1").hide(); } 
); 

ASP:

<td style="width:99px"> 
<asp:Menu ID="Menu1" BackColor="Purple" Orientation="Horizontal" ForeColor="White" runat="server" CssClass="Menu" StaticEnableDefaultPopOutImage="false">     
    <Items> 
    <asp:MenuItem NavigateUrl="index.aspx" Text="Hello1">        
    </asp:MenuItem>       
    </Items> 
</asp:Menu> 
</td> 
+0

它仍然沒有工作 – 2013-04-08 07:56:00

+0

@NgYangYang中當然它不會,沒有像td_1等東西閱讀我更新的答案:) – lifetimes 2013-04-08 08:06:17

+0

其實,它存在。我想要做的是id =「td_1」在下面的另一個表格中。 asp:菜單位於頂部的表格上,我想在下面的表格中隱藏一行,而mouseover事件位於另一個表格上。檢查我的代碼,你會明白我的意思。 – 2013-04-08 08:06:34

0

你需要一個ID選擇#有而不是類.

$("#Menu1").mouseover(
    //-^-- here 

,我認爲你所需要的就是hover()原因懸停有回調函數的mouseenter和鼠標離開

$("#Menu1").hover(
     function() { $("#td_1").show(); }, 
        //----^^--here 
     function() { $("#td_1").hide(); } 
        //----^^--here 
    ); 

注意:您需要一個seletor在td_1類或者id..and我不能使用這些名稱

+0

它仍然無法正常工作。 – 2013-04-08 07:55:20

+0

@NgYangYang更新檢查出來 – bipen 2013-04-08 07:58:41

+0

所有的元素只是在那裏,它是在不同的表 – 2013-04-08 08:12:35

0

您正在使用的,而不是ID

類找不到任何元素,您的Jquery應該看像:

$("#Menu1").mouseenter(function() { 
    $("td_1").show(); 
    }).mouseleave(function() { 
    $("td_1").hide(); 
    }); 
+0

它仍然無法正常工作。 – 2013-04-08 07:53:26

0

我不認爲你可以訪問這些標籤:td_1等,因爲它們不存在。代替 嘗試$('#td_1')...。而且不要忘了給項目的ID的td_{num}

編輯

或者使用$('td:nth-child({num})')$('#Menu1'),你給他們的ID螞蟻不是類