【问题标题】:Update javascript to newest version of JQuery将 javascript 更新到最新版本的 JQuery
【发布时间】:2014-02-03 04:40:49
【问题描述】:

可能是一个糟糕的标题,但不确定如何陈述问题。

我正在创建一个嵌套的网格视图。我使用的示例来自:http://www.aspsnippets.com/Articles/ASPNet-Nested-GridViews-GridView-inside-GridView-with-Expand-and-Collapse-feature.aspx

在您尝试使用 JQuery v1.10.2 运行它之前,使用示例显示/隐藏嵌套的 gridview 时效果很好。下面的脚本示例使用在 v1.7+ 中被贬值的 .live。所以我按照推荐更新到 .on 。问题是脚本的“减号”确实“删除”。似乎发生的是“加号”功能似乎再次触发。

这是原文:

        <script type="text/javascript">
        $(document).ready(function () {
            $("[src*=plus]").live("click", function () {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                $(this).attr("src", "../App_Themes/Theme1/minus.png");
            });
            $("[src*=minus]").live("click", function () {
                $(this).attr("src", "../App_Themes/Theme1/plus.png");
                $(this).closest("tr").after.remove();
            });
        });
</script>

她就是我更新的方式

    <script type="text/javascript">
        $(document).ready(function () {
            $("[src*=plus]").on("click", function () {
                $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                $(this).attr("src", "../App_Themes/Theme1/minus.png");
            });
            $("[src*=minus]").on("click", function () {

                $(this).attr("src", "../App_Themes/Theme1/plus.png");
                $(this).closest("tr").after.remove();
            });
        });

但还是不行。再次似乎 $("[src*=plus]") 只是一遍又一遍地触发。

图像从正值更新为负值。

【问题讨论】:

    标签: javascript jquery asp.net gridview


    【解决方案1】:

    你需要使用event delegation方法:

    $(document).ready(function () {
                $(document).on("click", "[src*=plus]", function () {
                    $(this).closest("tr").after("<tr><td></td><td colspan = '999'>" + $(this).next().html() + "</td></tr>")
                    $(this).attr("src", "../App_Themes/Theme1/minus.png");
                });
                $(document).on("click", "[src*=minus]", function () {
                    $(this).attr("src", "../App_Themes/Theme1/plus.png");
                    $(this).closest("tr").after.remove();
                });
            });
    

    【讨论】:

    • 我也有一个错误 $(this).closest("tr").after.remove();....应该是 $(this).closest("tr" ).next().remove();。谢谢!
    猜你喜欢
    • 2019-08-12
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2017-01-21
    • 1970-01-01
    相关资源
    最近更新 更多