asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-4 – 如何在asp.net MVC4查看页面中包含javascript代码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是asp.net MVC4架构的新手,我对以下事情感到困惑,请帮助我.
  1. @Scripts.Render("~/bundles/jquery")
  2. @Scripts.Render("~/bundles/jqueryui")
  3.  
  4. <script type="text/javascript">
  5. $(function () {
  6. $("#sections").tabs();
  7. //here i am getting error that Object[object object] has not method tabs
  8. });
  9. </script>
  10.  
  11. <div id="sections">
  12. <ul>
  13. <li><a href="#section-1">section-1 </a></li>
  14. <li><a href="#section-2">section-2 </a></li>
  15. </ul>
  16. <div id="section-1">
  17. section-1 content............
  18. </div>
  19. <div id="section-2">
  20. section-2 content............
  21. </div>
  22. </div>

提前致谢……

解决方法

您可能已经包含了〜/ bundles / jquery包两次.查看〜/ Views / Shared / _Layout.cshtml文件.最后你可能有以下内容
  1. @Scripts.Render("~/bundles/jquery")
  2. @RenderSection("scripts",required: false)
  3. </body>

所以jQuery包已经包含在你的页面中.您不应该在视图中第二次包含它.你只需要〜/ bundles / jqueryui包:

  1. @Scripts.Render("~/bundles/jqueryui")
  2.  
  3. <script type="text/javascript">
  4. $(function () {
  5. $("#sections").tabs();
  6. //here i am getting error that Object[object object] has not method tabs
  7. });
  8. </script>
  9.  
  10. <div id="sections">
  11. <ul>
  12. <li><a href="#section-1">section-1 </a></li>
  13. <li><a href="#section-2">section-2 </a></li>
  14. </ul>
  15. <div id="section-1">
  16. section-1 content............
  17. </div>
  18. <div id="section-2">
  19. section-2 content............
  20. </div>
  21. </div>

更新:

以下是视图结构可能如下所示的完整示例:

  1. @{
  2. Layout = null;
  3. }
  4.  
  5. <!DOCTYPE html>
  6.  
  7. <html>
  8. <head>
  9. <Meta name="viewport" content="width=device-width" />
  10. <title>Foo</title>
  11. <link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" />
  12. </head>
  13. <body>
  14. <div id="sections">
  15. <ul>
  16. <li><a href="#section-1">section-1 </a></li>
  17. <li><a href="#section-2">section-2 </a></li>
  18. </ul>
  19. <div id="section-1">
  20. section-1 content............
  21. </div>
  22. <div id="section-2">
  23. section-2 content............
  24. </div>
  25. </div>
  26.  
  27. @Scripts.Render("~/bundles/jquery")
  28. @Scripts.Render("~/bundles/jqueryui")
  29. <script type="text/javascript">
  30. $("#sections").tabs();
  31. </script>
  32. </body>
  33. </html>

猜你在找的asp.Net相关文章