简体   繁体   English

使用母版页的ASP.net MVC应用程序中的jQuery

[英]jQuery in an ASP.net MVC application using Master Pages

I am trying to get simple jQuery to execute on my Content page with no luck below is what I am trying to do: 我正在尝试让简单的jQuery在我的内容页面上执行,但以下是我想做的没有运气的事情:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

   <script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
   <script type="text/javascript">

       $(document).ready(function() {
           alert("hi");
       });   
   </script>

</asp:Content>

I have also tried getting the following to work: 我还尝试过以下工作:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

   <script src="../../Scripts/jquery-1.2.6.js" type="text/javascript"></script>
   <script type="text/javascript">
       function onload()
       {
          $("#myDiv").css("border", "1px solid green");
       }
   </script>

   <div id="myDive">
      Hello
   </div>

</asp:Content>

It may be that the JQuery file can't be found, try this for the script reference: 可能是找不到JQuery文件,请尝试以下操作作为脚本参考:

<script src="<%= Url.Content ("~/Scripts/jquery-1.2.6.js") %>" type="text/javascript"></script>

The Url.Content will build the correct path regardless of whether the app is running in the root or a sub-directory. 无论应用程序是在根目录还是在子目录中运行,Url.Content都将构建正确的路径。

Also, if you've installed the hot-fix for the JS intellisense, you can use this in addition to the above: 另外,如果您已经安装了JS intellisense的修补程序,则除了上述功能之外,还可以使用此修补程序:

<% if (false) { %>
    <!-- Don't wrap this is a Url.Content call, it's like this so we get intellisense! -->
    <script src="../../Scripts/jquery-1.2.6-vsdoc.js" type="text/javascript"></script>
<% } %>

Edit: 编辑:

Since the release of the RC 1 Refresh, there's been a know bug about placing elements with code blocks in the header, Philip Haacked has a nice article about solving it... 自RC 1 Refresh发布以来,存在一个关于在标头中放置带有代码块的元素的已知错误,Philip Haacked撰写了一篇不错的文章来解决它...

Edit 2: 编辑2:

Apparently this has been fixed since RC 2 was released... 显然,自RC 2发布以来,此问题已得到解决。

• Code nuggets that are direct children of the head element do not cause an exception if the runat="server" attribute is added to them. •如果将headat的直接子代的代码块添加到runat =“ server”属性,则不会导致异常。

Edit 3: 编辑3:

The hot-fix referenced earlier is only applicable to VS2008 and is available here - check out the blog post by the VS Web Dev Team here for details. 较早引用的热修复只适用于VS2008,并可在这里 -检查出由VS网络开发团队的博客文章在这里了解详情。 VS2010 has it built in. VS2010内置了它。

必须使用正确的网站发布,但是当您与只知道如何使用photoshop和dreamweaver的设计师一起工作时,这是一个痛苦

Try putting the javascript references in your Master Page. 尝试将javascript引用放入母版页中。 Then you don't have to worry about attempting to load the scripts multiple times. 然后,您不必担心尝试多次加载脚本。

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

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