添加jQuery的code到ASP.NET样板 [英] Adding jQuery code to ASP.NET Boilerplate

查看:81
本文介绍了添加jQuery的code到ASP.NET样板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用ASP.NET的样板项目。我想通过使用jQuery来添加更多的功能。

我试着写一个简单的code,如(下图)的页面(如Home.cshtml)结束时,它看起来像根本没有任何效果!然后,我尝试把这个code在Home.js文件,没有任何反应都没有。我也尝试创建一个新的.js与低于code文件,并将其链接到项目,体色没有变红!

请帮忙如何写jQuery的ASP.NET的样板。

  $(文件)。就绪(函数(){
    $(身体)的CSS(背景,红)。
});


解决方案

下面是过程正确执行;检查 @RenderSection @section 在下面的文件

_Layout.cshtml

 <!DOCTYPE HTML>
< HTML和GT;
< HEAD>
    &所述;! - .removed.for.brevity。 - >
    @ Styles.Render(〜/内容/ CSS)
    @ Scripts.Render(〜/包/ Modernizr的)< /头>
<身体GT;
    &所述;! - .removed.nav.bar.for.brevity。 - >    < D​​IV CLASS =箱体内容>
        @RenderBody()
        <小时/>
    &所述;! - .removed.footer.for.brevity。 - >
    < / DIV>    @ Scripts.Render(〜/包/ jQuery的)
    @ Scripts.Render(〜/包/引导)    <! - 。下面的脚本节将呈现来自视图CSHTML。 - >
    @RenderSection(脚本,必需:false)
< /身体GT;
< / HTML>

view.cshtml

 <! - 所有你查看HTML  - >
@section脚本{    <脚本>
      $(函数(){
          $(身体)的CSS(背景,红)。
      });
    < / SCRIPT>}

I have a project using ASP.NET Boilerplate. I want to add more functionality by using jQuery.

I try to write a simple code such as (below) to the end of a page (such as Home.cshtml), it looks like no effect at all! Then I try to put this code in Home.js file, nothing happens at all. I also try to create a new .js file with the below code and link it into the project, the body color didn't turn to red!

Please help how to write jQuery on ASP.NET Boilerplate.

$(document).ready(function () {
    $('body').css('background', 'red');
});

解决方案

Here is the process to proper execution; check @RenderSection and @section in below files

_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <!-- .removed.for.brevity. -->
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")

</head>
<body>
    <!-- .removed.nav.bar.for.brevity. -->

    <div class="container body-content">
        @RenderBody()
        <hr />
    <!-- .removed.footer.for.brevity. -->
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")

    <!-- .below script section will render from view cshtml. -->
    @RenderSection("scripts", required: false)
</body>
</html>

view.cshtml

<!-- All your view html -->
@section Scripts {

    <script>
      $(function () { 
          $('body').css('background', 'red'); 
      });
    </script>

}

这篇关于添加jQuery的code到ASP.NET样板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆