如何从Ajax调用的Div内调用Ajax调用?
问题描述:
我有一个PHP页面,是这样的:如何从Ajax调用的Div内调用Ajax调用?
下面是示意图: http://img233.imageshack.us/img233/4895/13423274.jpg
在DIV 1和DIV 2我做的Ajax调用的内容。 Ajax调用是由主页面创建的。 如果我想打一个电话从DIV 1更改DIV 2它不工作=/
我想:
document.getElementById('div2').innerHTML = data;
谁能帮助我?谢谢!
答
一个id不能以数字开头,这样可能会导致问题。
除此之外,它取决于您的JavaScript位于何处以及如何以及何时将其附加到您的事件处理程序。
如果JavaScript包含在DIV 1中加载的代码中,应该没有问题,但是如果它已经存在于页面中,则需要尽快将其附加到新加载的DIV 1中它被加载。
请参阅jQuery .live()
function documentation的详细解释我的意思。
答
因此,在DIV1的内容中,您有一些链接,比如想要将内容加载到DIV2中?然后你需要做的事情是将事件监听器添加到DIV1中的链接中,以便当它们被点击时,他们将创建AJAX调用,将内容加载到DIV2中。
我建议使用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/
对不起,我做错了,它调用Div2,但是,我不想使用Jquery这一个。似乎Div1内部无法访问div2,而div2内部我无法访问div1 – Loruaasd 2011-01-26 15:43:28