0

我很难找到答案,但没有成功。当按下按钮时,三个 div 应该以不同的速度/时间淡入。发生的情况是,当按下按钮时,页面似乎开始显示三个 div,但随后立即切换回来,导致它们从未真正出现。当我在不使用 masterpagefile 的常规 html 或 asp 页面中使用它时,它可以工作。Javascript 错误控制台不报告任何错误。代码如下:

switch_forms.aspx

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="switch_forms.aspx.cs" Inherits="swithforms.switch_forms" %>

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

   </asp:Content>
   <asp:Content ID="Content2" ContentPlaceHolderID="FeaturedContent" runat="server">
   </asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
<script>
    function switchForm() {
        $("#div1").fadeIn();
        $("#div2").fadeIn("slow");
        $("#div3").fadeIn(3000);
    };

</script>
<p>Demonstrate fadeIn() with different parameters.</p>
<button id="button" onclick="switchForm()">Click to fade in boxes</button>
<br><br>
<div id="div1" style="display: none;">
    <p>div1</p>
</div>
<br>
<div id="div2" style="display: none;">
    <p>div2</p>
</div><br>
<div id="div3" style="display: none;">
    <p>div3</p>
</div>
</asp:Content>

site.master/masterpagefile

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="swithforms.SiteMaster" %>

    <!DOCTYPE html>
    <html lang="en">
    <head runat="server">
    <meta charset="utf-8" />
    <title><%: Page.Title %> - My ASP.NET Application</title>
    <asp:PlaceHolder runat="server">     
      <%: Scripts.Render("~/bundles/modernizr") %>
</asp:PlaceHolder>  
<webopt:BundleReference runat="server" Path="~/Content/css" /> 
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<asp:ContentPlaceHolder runat="server" ID="HeadContent" />
</head>
<body>

<form runat="server">
<asp:ScriptManager runat="server">
    <Scripts>
        <%--To learn more about bundling scripts in ScriptManager see http://go.microsoft.com/fwlink/?LinkID=272931&clcid=0x409 --%>
        <%--Framework Scripts--%>

        <asp:ScriptReference Name="MsAjaxBundle" />
        <asp:ScriptReference Name="jquery" />
        <asp:ScriptReference Name="jquery.ui.combined" />
        <asp:ScriptReference Name="WebForms.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebForms.js" />
        <asp:ScriptReference Name="WebUIValidation.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebUIValidation.js" />
        <asp:ScriptReference Name="MenuStandards.js" Assembly="System.Web" Path="~/Scripts/WebForms/MenuStandards.js" />
        <asp:ScriptReference Name="GridView.js" Assembly="System.Web" Path="~/Scripts/WebForms/GridView.js" />
        <asp:ScriptReference Name="DetailsView.js" Assembly="System.Web" Path="~/Scripts/WebForms/DetailsView.js" />
        <asp:ScriptReference Name="TreeView.js" Assembly="System.Web" Path="~/Scripts/WebForms/TreeView.js" />
        <asp:ScriptReference Name="WebParts.js" Assembly="System.Web" Path="~/Scripts/WebForms/WebParts.js" />
        <asp:ScriptReference Name="Focus.js" Assembly="System.Web" Path="~/Scripts/WebForms/Focus.js" />
        <asp:ScriptReference Name="WebFormsBundle" />
        <%--Site Scripts--%>

    </Scripts>
</asp:ScriptManager>

<header>
    <div class="content-wrapper">
        <div class="float-left">
            <p class="site-title">
                <a runat="server" href="~/">your logo here</a>
            </p>
        </div>
        <div class="float-right">
            <section id="login">
                <asp:LoginView runat="server" ViewStateMode="Disabled">
                    <AnonymousTemplate>
                        <ul>
                            <li><a id="registerLink" runat="server" href="~/Account/Register">Register</a></li>
                            <li><a id="loginLink" runat="server" href="~/Account/Login">Log in</a></li>
                        </ul>
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        <p>
                            Hello, <a runat="server" class="username" href="~/Account/Manage" title="Manage your account">
                                <asp:LoginName runat="server" CssClass="username" /></a>!
                            <asp:LoginStatus runat="server" LogoutAction="Redirect" LogoutText="Log off" LogoutPageUrl="~/" />
                        </p>
                    </LoggedInTemplate>
                </asp:LoginView>
            </section>
            <nav>
                <ul id="menu">
                    <li><a runat="server" href="~/">Home</a></li>
                    <li><a runat="server" href="~/About">About</a></li>
                    <li><a runat="server" href="~/Contact">Contact</a></li>
                </ul>
            </nav>
        </div>
    </div>
</header>
<div id="body">
    <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
    <section class="content-wrapper main-content clear-fix">
        <asp:ContentPlaceHolder runat="server" ID="MainContent" />
    </section>
</div>
<footer>
    <div class="content-wrapper">
        <div class="float-left">
            <p>&copy; <%: DateTime.Now.Year %> - My ASP.NET Application</p>
        </div>
    </div>
</footer>
</form>
</body>
</html>

它一定与 .net 项目如何使用/加载母版页内容有关,但我太笨了,不知道。非常感谢您的帮助。谢谢!

4

1 回答 1

0

该按钮可能导致回发,尝试阻止这样的默认操作:

function switchForm(event) {
    event.preventDefault();
    $("#div1").fadeIn();
    $("#div2").fadeIn("slow");
    $("#div3").fadeIn(3000);
};

和你的处理程序,event作为参数添加:

onclick="switchForm(event)"
于 2013-06-14T21:01:07.700 回答