我有一个PHP页面,如下所示:
以下是原理图:http://img233.imageshack.us/img233/4895/13423274.jpg
在DIV 1和DIV 2中,我对内容进行了Ajax调用。Ajax调用是从主页发出的。如果我想从DIV 1调用更改DIV 2,它不起作用=/
我试过了:
document.getElementById('div2').innerHTML = data;
有谁可以帮我?谢谢!
发布于 2011-01-26 23:33:31
id不能以数字开头,因此这可能会导致问题。
除此之外,这取决于您的javascript所在的位置,以及您将其附加到事件处理程序的方式和时间。
如果javascript包含在DIV 1中加载的代码中,应该不会有问题,但如果它已经在页面上,则需要在加载后立即将其附加到新加载的DIV 1。
例如,有关我的意思的详细解释,请参阅jQuery .live()
function documentation。
发布于 2011-01-26 23:37:44
所以在DIV1的内容中你有一些链接,比如说,想要将内容加载到DIV2中?然后,您需要做的是向DIV1中的链接添加事件侦听器,以便当单击它们时,它们将创建将内容加载到DIV2中的AJAX调用。
我推荐使用jQuery (http://jquery.com/),您可以这样做:
// getting the content into DIV1
$( '#div1' ).load( '/path/to/backend.php', function()
{
// the ajax request is complete, so let's add listeners to the a tags in DIV1
$( '#div1 a' ).click( function()
{
// this clears DIV2's content
$( '#div2' ).empty();
// and now we load some ajax by getting the href of the a tag and passing that to our backend
$( '#div2' ).load( '/path/to/backend.php', { page: $( this ).attr( 'href' ) };
});
});
你可以在这里阅读更多内容:http://api.jquery.com/load/
https://stackoverflow.com/questions/4806327
复制相似问题