阿贾克斯不工作。我需要的页眉和页脚不会重新加载每次。你能帮助我吗? [英] Ajax not working. I need the header and footer not to reload every time. Can you help me?

查看:81
本文介绍了阿贾克斯不工作。我需要的页眉和页脚不会重新加载每次。你能帮助我吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的页眉和页脚不要在某些网页变化,所以这是很简单的ajax code我从stackoverflow.com复制。但它不工作。请帮我。

 < HTML>
    < HEAD>
        <脚本的src =// code.jquery.com / jQuery的-1.10.2.js>< / SCRIPT>
        <脚本类型=文/ JavaScript的>
            $(文件)。就绪(函数(){
                $(#活动)。点击(函数(){
                    $(#体)的负载(activities.html)。
                });
            });
        < / SCRIPT>
    < /头>
    <身体GT;
        < D​​IV ID =头>
            < A HREF =#ID =#活动>活动< / A>
            这是首部。
        < / DIV>
        < D​​IV ID =体与GT;
            &其中p为H.;
                这是身体。
            &其中p为H.;
        < / DIV>
        < D​​IV ID =页脚>

            这是页脚。

        < / DIV>
    < /身体GT;
< / HTML>
 

解决方案

您需要修改

 < A HREF =#ID =#活动>活动< / A>
 

 < A HREF =#ID =活动>活动< / A>
<!............. ^ .......................-->
 

注意:字符#是不是在属性值允许的ID

然后添加 即preventDefault() 以prevent浏览器默认操作上单击事件

  $(文件)。就绪(函数(){
    $(#活动)。点击(函数(五){
        即preventDefault();
        $(#体)的负载(activities.html)。
    });
});
 

I need header and footer not to change in some webpages so this is simple ajax code I have copied from stackoverflow.com. But it is not working. Please help me.

<html>
    <head>
        <script src="//code.jquery.com/jquery-1.10.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function(){
                $("#activities").click(function(){
                    $("#body").load("activities.html");
                });
            });
        </script>
    </head>
    <body>
        <div id="header">
            <a href="#" id="#activities">Activities</a>
            This is the header.
        </div>
        <div id="body">
            <p>
                This is the body. 
            <p>
        </div>
        <div id="footer">

            This is the footer.

        </div>
    </body>
</html>

解决方案

You need to change

<a href="#" id="#activities">Activities</a>

to

<a href="#" id="activities">Activities</a>
<!.............^.......................-->

Note : character "#" is not allowed in the value of attribute "ID"

Then add e.preventDefault() to prevent the browser default action on click event

$(document).ready(function() {
    $("#activities").click(function(e) {
        e.preventDefault();
        $("#body").load("activities.html");
    });
});

这篇关于阿贾克斯不工作。我需要的页眉和页脚不会重新加载每次。你能帮助我吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆