Register On-Click Event with jQuery - Not Firing

后端 未结 2 734
温柔的废话
温柔的废话 2021-01-22 00:38

Should be a quickie ...

1) I created a new project in VS2013 using MVC.
2) I added the following code to the bottom of the default Home\\Index.cshtml that is created

相关标签:
2条回答
  • 2021-01-22 01:15

    According to w3.org (emphasis mine):

    The script may be defined within the contents of the SCRIPT element or in an external file. If the src attribute is not set, user agents must interpret the contents of the element as the script. If the src has a URI value, user agents must ignore the element's contents and retrieve the script via the URI.

    That's to say, you can't specify both a src and a body for your script element and expect both to work... so, use two separate script elements.

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
    <script>
        $(document).on('click', '.myalertclass', function (e) {
            alert('Hello you');
        })
    </script>
    
    0 讨论(0)
  • 2021-01-22 01:31

    You can not include inline scripts in a referenced script tag. Try this:

    <script src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>
    
    <script>
        $(document).on('click', '.myalertclass', function (e) {
            alert('Hello you');
        })
    </script>
    
    0 讨论(0)
提交回复
热议问题