Php AJAX:如何用同一DIV中的链接替换DIV的内容?

Php AJAX:如何用同一DIV中的链接替换DIV的内容?,php,ajax,json,html,hyperlink,Php,Ajax,Json,Html,Hyperlink,我有这样的index.php: <script type="text/javascript" src="jquery-1.4.2.js"></script> <script type="text/javascript" src="ajax.js"></script> <a href='one.php' class='ajax'>One</a> <div id="workspace">workspace<

我有这样的index.php:

<script type="text/javascript" src="jquery-1.4.2.js"></script>
<script type="text/javascript" src="ajax.js"></script>

<a href='one.php' class='ajax'>One</a>

<div id="workspace">workspace</div>
加载index.php时,会有一个链接(一个)和一个DIV(工作区)。当我点击'One'链接时,调用One.php的内容,链接'Two'成功出现在'workspace'DIV中

但现在,当我单击链接'Two'时,“helloworld”不会出现在'workspace'DIV中,并且在没有AJAX调用的情况下单独打开

如何强制此代码使用带有上述流的链接“2”将workspace DIV的内容替换为“Hello World”?


谢谢

问题在于,使“.ajax”链接如此神奇的init方法只在加载文档时被调用一次。当你替换html时,魔法就消失了

试试这个:

function assignLinks() {

    jQuery('.ajax').click(function(event) {
            event.preventDefault();
            // load the href attribute of the link that was clicked
            jQuery.getJSON(this.href, function(snippets) {
                for(var id in snippets) {

                    // updated to deal with any type of HTML
                    jQuery('#' + id).html(snippets[id]).click( function() {
                        assignLinks();
                    });
                }
            });
        });

}

jQuery(document).ready(function() {
    assignLinks();        
});
或者,只需使用live(如下所述)。

尝试该方法,而不是单击

jQuery('.ajax').live('click', function(event) {
  // your code
});

live
在您分配事件的内容存在或稍后添加/加载时使用。

啊,您说得对。问题在于,查看“.ajax”的init方法只在加载文档时被调用一次(因此后续的更改不会受到影响)。将在一秒钟内更新。。。
jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event) {
        event.preventDefault();
        // load the href attribute of the link that was clicked
        jQuery.getJSON(this.href, function(snippets) {
            for(var id in snippets) {
                // updated to deal with any type of HTML
                jQuery('#' + id).html(snippets[id]);
            }
        });
    });
});
function assignLinks() {

    jQuery('.ajax').click(function(event) {
            event.preventDefault();
            // load the href attribute of the link that was clicked
            jQuery.getJSON(this.href, function(snippets) {
                for(var id in snippets) {

                    // updated to deal with any type of HTML
                    jQuery('#' + id).html(snippets[id]).click( function() {
                        assignLinks();
                    });
                }
            });
        });

}

jQuery(document).ready(function() {
    assignLinks();        
});
jQuery('.ajax').live('click', function(event) {
  // your code
});