从VB.NET背后的代码调用单击事件或JQuery函数

Calling a click event or JQuery function from the code behind VB.NET

本文关键字:事件 单击 JQuery 函数 调用 代码 VB NET 背后      更新时间:2023-09-26

我有一个JQuery(v1.8),它在单击超链接后处理代码。我想调用该点击或做任何其他事情(就像点击链接一样)来强制JQuery从代码后面运行。有什么想法吗?

JQuery代码:

<script type="text/javascript">
jQuery(document).ready(function (){    
    jQuery('#lnkShowModule').toggle( 
        function (){ 
            jQuery(this).html('Hide the Module'); 
            jQuery('.hide-element').toggle(); 
        }, 
        function (){ 
            jQuery(this).html('Show the Hidden Module'); 
            jQuery('.hide-element').toggle(); 
        }
    );
});

这是我在ascx控件中的链接:

<a id="lnkShowModule" href="#"> show the hidden module</a>

有什么想法吗?

在函数中添加javascript代码,您可以在任何想要调用javascript函数的地方添加以下代码

Page.ClientScript.RegisterStartupScript(Page.GetType(), "ShowHide", "ShowHideDiv();", true);

#UPDATE 1

function ShowHideDiv(){
$(document).ready(function (){    
    $('#lnkShowModule').toggle( 
        function (){ 
            $(this).html('Hide the Module'); 
            $('.hide-element').toggle(); 
        }, 
        function (){ 
            $(this).html('Show the Hidden Module'); 
            $('.hide-element').toggle(); 
        }
    );
});
}

你还必须更改"a"

<a id="lnkShowModule" href="#" onclick="ShowHideDiv();"> show the hidden module</a>

如果你已经在发帖了(正如你在问题中指出的,你想从代码后面完成这项工作),那么你也可以简单地从代码后面设置有问题的对象的可见性。

如果您的意图是隐藏一个div,您可以将它变成一个asp:Panel(MyPanel)(它输出一个div)。然后你可以简单地拨打:

MyPanel.Visible = False; ' or True

以设置可见性。不需要javascript/jquery。