简体   繁体   English

如何使用jQuery在div中加载php页面

[英]how to load php page in a div using jquery

Imagine that I have two divs , left div and right div , 想象一下,我有两个div,左div和右div,

In the left div I have some button. 在左侧的div中,我有一些按钮。 When I click on the home button the page home.php loaded in the right div, I use load() function to do this and when I click on about button the page about.php loaded in the right div . 当单击主页按钮时,在右div中加载了home.php页面,我使用load()函数执行此操作;当单击按钮about时,在右div中加载了about.php页面。

My problem is that I have some other buttons in that page (I mean about.php). 我的问题是该页面上还有其他一些按钮(我的意思是about.php)。 When I click on it some other pages must load in the right div. 当我单击它时,必须在正确的div中加载其他页面。

I mean when I click on about button in the left div, about.php loaded in the right div. 我的意思是,当我单击左侧div中的about按钮时,在右侧div中加载了about.php

I don't have a problem doing this it's work fine , but the problem is when i click on a button in about.php , I want about.php to be hide and load x.php in his place (right div). 我这样做没有问题,但工作正常,但问题是当我单击about.php的按钮时,我希望about.php隐藏并在他的位置(右div)加载x.php

This is the code that I have : 这是我的代码:

$('#about').click(function () {
   $('#rightdiv').load('about.php', function() {
      $('#about_button').click(function () {
          $('#rightdiv').load('x.php');
      })
   })
})

First of all, you shouldn't nest your click handlers like you have done - they should be separate. 首先,您不应像完成操作那样嵌套您的点击处理程序-它们应该分开。 Second, when you load the about.php jQuery is unaware of the new stuff that you have placed in the DOM. 其次,当您加载about.php时,jQuery不会意识到放置在DOM中的新内容。 To fix that you use event delegation, meaning the click event from about.php will bubble up to an element that already existed at the time the page was loaded. 要解决此问题,请使用事件委托,这意味着about.php中的click事件将冒泡到页面加载时已经存在的元素。 jQuery is aware of that element and will handle the event properly. jQuery知道该元素,并将正确处理该事件。

$('#about').click(function () {
   $('#rightdiv').load('about.php');
});

// 'click' event delegated to 'body'
$('body').on('click', '#about_button', function () { // #about_button is in about.php
   $('#rightdiv').load('x.php');
})

Instead of binding the event to the items on load, you tell the browser to check ther a.nav 's which occur in the document . 无需将事件绑定到加载的项目,而是告诉浏览器检查document中出现的a.nav

<a class="nav" href="about.php">about.php</a>
<a class="nav" href="other.php">other.php</a>
<a class="nav" href="last.php">last.php</a>

$(document).on('click', 'a.nav',function (e) {
    e.preventDefault();
    var page = this.href;
    $('#rightdiv').load(page);
})

It's better to make the first selecttor ( $(document) ) as close to the actual elements, and highly recommended you use an ID: 最好使第一个选择器( $(document) )尽可能靠近实际元素,强烈建议您使用ID:

$('#rightdiv').on('click', 'a.nav',function (e) {});

In this snippet, all a.nav in #rightdiv will get triggered, initial load or dynamicly. 在这个片段中,所有a.nav#rightdiv将被触发,初始加载或动态地。

您必须在加载该php页面后绑定所有click事件,因此创建一个函数,该函数将在每个.load调用之后执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM