【问题标题】:Problems making site not reload when tabs are accessed访问选项卡时导致网站无法重新加载的问题
【发布时间】:2014-09-25 03:44:00
【问题描述】:

我正在尝试创建一个函数,让用户无需重新加载页面即可访问已点击选项卡的内容。

一开始我以为是这样的

$( "#tab1" ).click(function() {
    $("#content").load("tab1.html #content > *");
});

可以正常工作,但不能 - 页面仍会重新加载,因此让我很困惑。

Bootply.

有人知道我的问题出在哪里吗?

HTML:

<div class="custom-wrapper"> 
<div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">

            <div class="collapse navbar-collapse navHeaderCollapse">

                <ul class="nav navbar-nav navbar-right">

                   <li><a id="tab1" href="tab1.html">Tab1</a></li>
                    <li><a id="tab2" href="tab2.html">Tab2</a></li>

                </ul><!-- END: "collapse navbar-collapse navHeaderCollapse" -->
            </div><!-- END: "container" -->
        </div><!-- END: "container" -->
    </div><!-- END: "navbar navbar-inverse navbar-fixed-top" -->

<div class="container pad-container">
<div id="content">
         <h1>Content</h1>
         <p>stuff...</p>
         <p>lorem lorem</p>
         <p>lorem lorem</p>
         <p>lorem lorem</p>
         <p>lorem lorem</p>
  </div>

    <div class="footer static-footer">
        <div class="container">             

        </div><!-- END: "container" --> 
    </div><!-- END: "footer static-footer" -->  
</div><!-- END: "container pad-container" -->   

CSS:

html,
body {
height:100%;
font-family: 'Open Sans', sans-serif;
}
.custom-wrapper {
min-height:100%;
position:relative;
}
.pad-container{
padding-top:80px;
padding-bottom:80px; /*height of the footer plus a little */
}

.static-footer {
position:absolute;
bottom:0;
left:0;
width:100%;
height:50px;   /* height of the footer */
background:#000000;
}
.static-footer .text-muted { /* text on left of footer */
color: #ffffff; 
margin-top: 17px;
}
.static-footer .text-primary{ 
margin-top: -33px;
}

【问题讨论】:

    标签: jquery html css ajax dynamic


    【解决方案1】:

    取消默认点击事件:

    $( "#tab1" ).click(function(e) {
        e.preventDefault();
        $("#content").load("tab1.html #content");
    });
    

    http://api.jquery.com/event.preventDefault/

    【讨论】:

    • 现在,点击 Tab1 对我来说毫无用处。这是为什么呢?
    • 也许你的load 失败了?检查控制台,验证 URL 是否正确。
    • 我不知道如何调试它,因为我找不到任何错误。嗯,这是使用 jquery 1.7.2 的功能吗?
    • 改用$("#content").load("tab1.html #content") -- api.jquery.com/load/#loading-page-fragments
    • 这很奇怪 - 使用 jQuery 1.11.1 使它工作。但是,我无法判断它是否实际上是在不重新加载的情况下加载内容,因为它仍然给人一种重新加载的“错觉”。有没有办法检查它是否真的有效?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 2019-08-10
    • 2018-10-22
    • 2013-08-18
    相关资源
    最近更新 更多