JQuery click method on asp.net Menu item?

隐身守侯 提交于 2019-12-11 10:25:11

问题


I have asp.net Menu Item <asp:MenuItem NavigateUrl="" Text="Download" Value="Download"/>. When this item gets clicked, I want to execute jQuery click method. How can we do that?


回答1:


Provide a CssClass property with some class to the menu item. On client side find the element using that class and attach click event handler to it.

<asp:MenuItem NavigateUrl="" CssClass="menuItem" Text="Download" Value="Download"/>

Js

$('.menuItem').click(function(){
    //do stuff here
});

Update:

I think you can specify the css class in this way.

<asp:Menu ID="mainMenu" runat="server">
    ..
    <asp:MenuItem NavigateUrl="" CssClass="menuItem" Text="Download" Value="Download"/>
    ..
    <StaticMenuItemStyle CssClass="menuItem" />
</asp:Menu>



回答2:


Sample JQuery

<script language="javascript" type="text/javascript">
   $(function () {
        $(".MyMenu a").each(function (index) {
             $(this).click(function () {
                 alert(index);
                 return false;
             });
        });
   });
</script>

Modified HTML

<asp:Menu ID="_mainMenu" RenderingMode="Table" runat="server" CssClass="MyMenu" autopostback="true">
    <Items>
        <asp:MenuItem Text="Home" Value="Home"></asp:MenuItem>
        <asp:MenuItem Text="Index" Value="Home"></asp:MenuItem>
    </Items>
</asp:Menu>

Note - RenderingMode="Table"



来源:https://stackoverflow.com/questions/9928488/jquery-click-method-on-asp-net-menu-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!