【问题标题】:Why don't jQuery works in Microsoft Visual Studio 2010?为什么 jQuery 在 Microsoft Visual Studio 2010 中不起作用?
【发布时间】:2012-01-06 12:07:05
【问题描述】:

我在 MS Visual Studio 2010 中创建了一个 ASP.NET MVC 2 Web 应用程序。现在我需要使用 JQuery 为其添加样式。为了在 div 中创建图像幻灯片,我编写了一个 JQuery 文件并尝试了将其与我的查看母版页链接。但是发生的情况是,当我尝试运行我的项目时,Visual Studio 告诉“Microsoft JScript 运行时错误:'jQuery' 未定义”。你们中的任何人都可以帮助我解决这个问题吗? ...谢谢:)

here's the code I have given within my jQuery

/*Coding : Nidhin & Eldhose*/


function slideSwitch() {
var $active = $('#Left_Image DIV.active');

if ($active.length == 0) $active = $('#Left_Image DIV:last');

// use this to pull the divs in the order they appear in the markup
var $next = $active.next().length ? $active.next()
: $('#Left_Image DIV:first');

// uncomment below to pull the divs randomly
//     var $sibs  = $active.siblings();
//     var rndNum = Math.floor(Math.random() * $sibs.length );
//     var $next  = $( $sibs[ rndNum ] );


$active.addClass('last-active');

$next.css({ opacity: 0.0 })
.addClass('active')
.animate({ opacity: 1.0 }, 1000, function () {
$active.removeClass('active last-active');
});
}

$(function () {
setInterval("slideSwitch()", 5000);
});

【问题讨论】:

  • 请检查您是否在该页面上包含了 jQuery 库。
  • 使用谷歌浏览器开发者工具。太棒了。在谷歌浏览器中按 F12 并转到控制台选项卡。如果您收到任何脚本错误,您将看到。他们会指导你。

标签: jquery css asp.net-mvc


【解决方案1】:

您可能在尝试使用之前忘记引用 jquery 脚本:

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

【讨论】:

    【解决方案2】:

    确保您已包含 jQuery。

    你的母版页应该有类似的东西

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    

    理想情况下,脚本应该最后加载,但请确保在尝试使用之前已加载 jQuery。

    【讨论】:

    • 现在我遇到了另一个错误,提示“Microsoft JScript 运行时错误:'$' 未定义”
    • 这确实很奇怪。 “jquery is undefined”和“$ is undefined”是它抛出的错误,当(你猜对了),jQuery 没有被正确包含。您确定在脚本中使用 jQuery 之前包含它吗?尝试在 &lt;head /&gt; 中包含 jQuery。
    • 另外,我昨天也遇到了类似的问题,结果是 OpenDNS 屏蔽了 Google CDN。尝试从不同的 CDN 包含它,例如 jquery.com,甚至本地。
    【解决方案3】:

    埃尔多,

    我建议您重新访问 slideSwitch() 方法并首先解决这类事情。我什至建议将 slideSwitch() 删除为:

    function slideSwitch() {
        var $active = $('#Left_Image DIV.active');
        alert($active);
    }
    

    从那里一点一点地拿走它。此外,使用萤火虫查看幕后发生的事情。最后,正如一些 cmets 中提到的,尝试加载 jquery 的本地副本,而不是 CDN 版本,以防内容 DNS 等出现问题。

    [edit] - 忘了补充,确保您的 jquery js 文件是按顺序加载的 BEFORE 您的自定义 js 文件!即:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="<%= Url.Content("~/Scripts/myscript.js") %>"></script>
    

    【讨论】:

      【解决方案4】:

      您的视图中是否嵌入了脚本?如果是这样,这可能会导致问题。

      当呈现页面并且您的视图脚本按顺序呈现时会发生此问题 在布局页面中的脚本之前。

      例如,如果您在 _Layout.cshtml 页面底部引用 jquery

      </div>
      <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
      </body>
      </html>
      

      并且您正在引用例如您的视图中的 jquery 验证

      <h2>Edit</h2>
      
      <script src="<%: Url.Content("~/Scripts/jquery.validate.min.js") %>"    type="text/javascript"></script>
      <script src="<%: Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") %>"></script>
      
      @using (Html.BeginForm())
      

      您可以将视图中的脚本移动到脚本部分

      <h2>Edit</h2>
      
      @section scripts {
         <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
         <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
      }
      
      @using (Html.BeginForm())
      

      并在您的布局中引用脚本部分,传递第二个参数,该参数指示脚本部分是否是可选的。

         ...
         </div>
         <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
         @RenderSection("section", false) 
       </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多