【问题标题】:JQuery script is not working in ASP.NET MVCJQuery 脚本在 ASP.NET MVC 中不起作用
【发布时间】:2018-11-18 14:38:23
【问题描述】:

我想要一个按钮来处理某个 JQuery 函数,但它什么也没做。

我的 .cshtml 文件中的脚本:

<script src="~/lib/jquery/dist/jquery.min.js">
    jQuery(function ($) {
        $('#swapHeart').on('click', function () {
            var $el = $(this),
                textNode = this.lastChild;
            $el.find('span').toggleClass('glyphicon-heart glyphicon-heart-empty');
            $el.toggleClass('swap');
        });
    });
</script>

以及我的 .cshtml 文件中的按钮:

<button id="swapHeart" class="btn btn-default swap">
    <span class="glyphicon glyphicon-heart-empty"></span>
</button>

为什么这不起作用?提前致谢。 编辑:它在我创建的这个 codepen 中工作:https://codepen.io/floorvrmt/pen/qQPEKQ

【问题讨论】:

    标签: jquery html asp.net-mvc


    【解决方案1】:

    您的代码使用与加载 jQuery 源代码相同的脚本标签。检查下面的代码,我在其中加载了源代码和脚本

    <script src="~/lib/jquery/dist/jquery.min.js">
    </script>
    <script>
        jQuery(function ($) {
            $('#swapHeart').on('click', function () {
                var $el = $(this),
                    textNode = this.lastChild;
                $el.find('span').toggleClass('glyphicon-heart glyphicon-heart-empty');
                $el.toggleClass('swap');
            });
        });
    </script>
    

    【讨论】:

      猜你喜欢
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多