繁体   English   中英

如何从ScriptManager.RegisterStartupScript调用JavaScript函数?

[英]How to call javascript function from ScriptManager.RegisterStartupScript?

我有以下javascript代码:

<script type="text/javascript" language="javascript">
        $(document).ready(
            function () {
                // THIS IS FOR HIDE ALL DETAILS ROW
                $(".SUBDIV table tr:not(:first-child)").not("tr tr").hide();
                $(".SUBDIV .btncolexp").click(function () {
                    $(this).closest('tr').next('tr').toggle();
                    //this is for change img of btncolexp button
                    if ($(this).attr('class').toString() == "btncolexp collapse") {
                        $(this).addClass('expand');
                        $(this).removeClass('collapse');
                    }
                    else {
                        $(this).removeClass('expand');
                        $(this).addClass('collapse');
                    }
                });

                function expand_all() {
                    $(this).closest('tr').next('tr').toggle();
                    };
            });

    </script>

我想通过code-behind调用expand_all函数。

我知道我可以使用类似这样的东西,但是它不起作用,而且我不理解所使用的参数:

ScriptManager.RegisterStartupScript(Me, GetType(String), "Error", "expand_all();", True)

你能帮助我吗?

您的方法expand_all仅存在于$(document).ready(...)内部函数的范围内,以便您从ScriptManager.RegisterStartupScript调用它,该方法必须位于窗口级别,只需将该函数移到窗口之外即可。 $(document).ready(...)

<script type="text/javascript" language="javascript">
    $(document).ready(
        function () {
           ....                
        });

    function expand_all() {
           $(this).closest('tr').next('tr').toggle();
    };
</script>

因为您在匿名$.ready事件上下文中定义了expand_all函数。 将您的代码放在外面,它应该可以工作。

function expand_all(){
    alert('test B');
}
$(document).ready(
    function () {
        // this won't work
        function expand_all() {
            alert('test A');
        };
    });


// will show test B
expand_all();

检查一下:

http://jsfiddle.net/jrrymg0g/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM