0

我正在尝试在我的项目中使用手风琴。据我了解,应该在布局页面中添加 javascript 链接,在索引页面中添加 javascript 代码,它必须在其中显示。但是浏览器说 $(#"accordion").accordion 不是一个函数。这是_Layout的代码。

@using kazwaySite
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" 
        @Styles.Render("~/Content/themes/base/css", "~/Content/css")
        @Scripts.Render("~/bundles/modernizr")

   <link type="text/css" href="~/Scripts/css/ui-lightness/jquery-ui-1.8.22.custom.css" rel="stylesheet" />  
   <script type="text/javascript" src="@Url.Content("~/Scripts/js/jquery-1.7.2.min.js")"></script>
   <script type="text/javascript" src="@Url.Content("~/Scripts/js/jquery-ui-1.8.22.custom.min.js")"></script>

    </head>
    <body>
        <header>
            <div class="content-wrapper">
                <div class="float-left">
                    <p class="site-title">@Html.ActionLink("Your logo here", "Index", "Home")</p>
                </div>
                <div class="float-right">
                    <section id="login">
                        @Html.Partial("_LoginPartial")
                    </section>
                    <nav>
                        <ul id="menu">
                            <li>@Html.ActionLink(Resources.mainPage , "Index", "Home")</li>
                            <li>@Html.ActionLink(Resources.aboutPage, "About", "Home")</li>
                            <li>@Html.ActionLink(Resources.productsPage, "Index", "Home")</li>
                            <li>@Html.ActionLink(Resources.capabilitiesPage, "Index", "Home")</li>
                            <li>@Html.ActionLink(Resources.newsPage, "About", "Home")</li>
                            <li>@Html.ActionLink(Resources.partnersPage, "Index", "Home")</li>
                        </ul>
                    </nav>
                </div>
            </div>
        </header>
        <div id="body">
            @RenderSection("featured", required: false)
            <section class="content-wrapper main-content clear-fix">
                @RenderBody()
            </section>
        </div>
        <footer>
            <div class="content-wrapper" >
            <table >
            <tr style="font-size:20px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>
            <tr style="font-size:12px;font-weight:500;">
            <td>@Html.ActionLink("О компании", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            <td>@Html.ActionLink("Возможности", "About", "Home")</td>
            <td>@Html.ActionLink("Новости", "About", "Home")</td>
            <td>@Html.ActionLink("О продукции", "About", "Home")</td>
            </tr>

            </table>
                <div class="float-left">
                    <p>&copy; @DateTime.Now.Year - ONM Web studio</p>
                </div>
                <div class="float-right">
                    <ul id="social">
                        <li><a href="http://facebook.com" class="facebook">Facebook</a></li>
                        <li><a href="http://twitter.com" class="twitter">Twitter</a></li>
                    </ul>
                </div>
            </div>
        </footer>

        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

index.cshtml的代码

@model IEnumerable<kazwaySite.Models.News>


    <script type="text/javascript">
        $(function () {
            $("#accordion").accordion({ header: "h3" });
        });
   </script>
        <div id="accordion">
            <div>
                <h3><a href="#">First</a></h3>
                <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div>
            </div>
            <div>
                <h3><a href="#">Second</a></h3>
                <div>Phasellus mattis tincidunt nibh.</div>
            </div>
            <div>
                <h3><a href="#">Third</a></h3>
                <div>Nam dui erat, auctor a, dignissim quis.</div>
            </div>
        </div>
4

2 回答 2

2

如果未定义函数手风琴,则不加载 jquery ui。检查浏览器,该链接指向正确的位置。

于 2012-07-27T12:19:45.063 回答
1

你已经把你的javascript包含弄得一团糟。您已经多次包含 jquery(一次在头部,一次在底部),您没有覆盖您在视图内的布局中定义的脚本部分,...

让我们试着清理一下。由于这是一个 ASP.NET MVC 4 应用程序,我建议您使用捆绑包。开箱即用有许多捆绑包,但您会得到一个 forjquery和一个 for jqueryui。这2个就够了。您不需要单独包含捆绑包和脚本。这完全违背了捆绑包的目的,并且您经常会得到欺骗脚本,......所以继续您的~/App_Start/BundleConfig.cs文件并定义您想要使用的捆绑包。如果要使用更新版本的脚本,请更新 NuGet。归根结底,您应该在文件夹中拥有所需的脚本~/Scripts并为它们配置捆绑 ID。

@using kazwaySite
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title - My ASP.NET MVC Application</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" 
        @Styles.Render("~/Content/themes/base/css", "~/Content/css")
        <link type="text/css" href="~/Scripts/css/ui-lightness/jquery-ui-1.8.22.custom.css" rel="stylesheet" />  
    </head>
    <body>
        ... some markup ommited for clarity ...

        @Scripts.Render("~/bundles/jquery")
        @Scripts.Render("~/bundles/jqueryui")
        @RenderSection("scripts", required: false)
    </body>
</html>

在你的视野内:

@model IEnumerable<kazwaySite.Models.News>

@section scripts {
    <script type="text/javascript">
        $("#accordion").accordion({ header: "h3" });
    </script>
}

<div id="accordion">
    <div>
        <h3><a href="#">First</a></h3>
        <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div>
        </div>
    <div>
        <h3><a href="#">Second</a></h3>
        <div>Phasellus mattis tincidunt nibh.</div>
    </div>
    <div>
        <h3><a href="#">Third</a></h3>
        <div>Nam dui erat, auctor a, dignissim quis.</div>
    </div>
</div>
于 2012-07-27T12:20:10.460 回答