How to use jquery in ASP.​NET Core

后端 未结 3 550
一个人的身影
一个人的身影 2020-12-29 23:19

I created a ASP.NET core template and wrote a jquery script. When I look at the page I see that jquery is loaded into the page, but the script doesn’t run. I looked at the A

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 23:47

    If you create a new .Net Core 2.2 - Web Application with Razor Pages and try to add a jQuery script in index.cshtml you will get the error Uncaught ReferenceError: $ is not defined. As already mentioned this is because jQuery is loaded after the rest of the page content.

    Fix this by navigating to Pages\Shared\_Layout.cshtml and add the scripts section to the html head tag instead. Then it will work:

    Example:

    Move from below :

    
        
        
    
    
        
        
    
    
    
    @RenderSection("Scripts", required: false)
    

    Into , should look like this:

    
        
        
        @ViewData["Title"] - JiraApp.Clients.Web
    
        
            
        
        
            
        
        
        
            
            
        
        
            
            
        
        
    
        @RenderSection("Scripts", required: false)
    
    

提交回复
热议问题