简体   繁体   English

jQuery脚本在ASP.NET MVC中不起作用

[英]JQuery script is not working in ASP.NET MVC

I want a button to handle a certain JQuery function, but it is not doing anything. 我想要一个按钮来处理某个JQuery函数,但是它什么也没做。

The script inside my .cshtml file: 我的.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>

and the button in my .cshtml file: 和我的.cshtml文件中的按钮:

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

Why is this not working? 为什么这不起作用? Thanks in advance. 提前致谢。 EDIT: It does work in this codepen I created: https://codepen.io/floorvrmt/pen/qQPEKQ 编辑:它确实在我创建的这个Codepen中起作用: https ://codepen.io/floorvrmt/pen/qQPEKQ

Your code using the same script tag which is used to load the source of jQuery. 您的代码使用用于加载jQuery源的相同脚本标签。 Check the code below, in which I load the source and script 检查下面的代码,在其中加载源代码和脚本

<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>

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

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