I made an ajax website calling pages from /pages
folder inside an ajax-container
div in my index.php
.
我创建了一个ajax网站,在我的index.php中的ajax-container div中调用/ pages文件夹中的页面。
Now i want to make a second ajax request after the first ajax request success but the second request will be only on certains page,
现在我想在第一个ajax请求成功后发出第二个ajax请求,但第二个请求只会在某些页面上,
for example: i call the page work.php
and inside this page i want to call a page work-slider
from the same folder and replace the work.php page by work-slider.php in my ajax-container div when i click on images link-in
but i can't find a solution for make it,
例如:我调用页面work.php,在这个页面里面我想从同一个文件夹中调用一个页面工作滑块,当我点击时,在我的ajax-container div中用work-slider.php替换work.php页面图像链接但我找不到制作它的解决方案,
This is my actual code:
这是我的实际代码:
index.php:
<div id="ajax-container">
<?php
$d = "pages/";
if (isset($_GET['p'])) {
$p = strtolower($_GET['p']);
if (preg_match("/^[a-z0-9\-]+$/", $p) && file_exists($d . $p . ".php")) {
include $d . $p . ".php";
} else {
include $d . "404.php";
}
} else {
include $d . "home.php";
}
?>
</div>
Ajax function:
var afficher = function(data) {
$('#ajax-container').fadeOut(250, function() {
$('#ajax-container').empty();
$('#ajax-container').append(data);
$('#ajax-container').fadeIn(100, function() {});
});
};
var lastRequest = null;
if (lastRequest !== null) {
lastRequest.abort();
}
var loadPage = function(page, storeHistory) {
if (typeof storeHistory === 'undefined') {
storeHistory = true;
}
lastRequest = $.ajax({
url: "pages/" + page,
cache: false,
success: f$.ajax({
url: "pages/" + page,
cache: false,
success: function(resultFirst) {
afficher(resultFirst);
if (storeHistory === true) {
history.pushState({
'key': 'value',
'url': page
}, '', page);
}
$.ajax({
// How can i define pageIn variable ??
url: "pages/" + pageIn,
cache: false,
success: function(resultSecond) {
afficher(resultSecond);
}
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
afficher('erreur lors du chagement de la page');
}
});
return false;
};
window.addEventListener('load', function() {
setTimeout(function() {
window.addEventListener('popstate', function(e) {
if (e.state === null) {
loadPage('home.php');
} else {
loadPage(e['state']['url'], false);
}
});
}, 0);
});
// link inside index.php
$('.link').bind('click', function(e) {
e.preventDefault();
var page = $(this).attr('href');
loadPage(page);
return false;
});
// second link inside called page
$('.link-in').bind('click', function(e) {
e.preventDefault();
var pageIn = $(this).attr('href');
loadPage(pageIn);
return false;
});
1 个解决方案
#1
0
If that .link-in tag if dinamically inserted into the DOM after the page loads, the bind() method will fail: bind() attach listeners to elements it find when called. If you add a new elemenent after the bind call, it will not receive the listener binding.
如果.link-in标记在页面加载后以语法形式插入到DOM中,则bind()方法将失败:bind()将侦听器附加到调用时找到的元素。如果在绑定调用后添加新的元素,它将不会收到侦听器绑定。
In order to prevent that when dinamically inserting html code in a page, you should use the on() method:
为了防止在页面中插入html代码时,应该使用on()方法:
$('body').on('click' , '.link-in', function(e) {
e.preventDefault();
var pageIn = $(this).attr('href');
loadPage(pageIn);
return false;
});
And now, every .link-in tag you push dinamically inside body will respond to the click listener. A discussion on using on() by default instead of bind() is in fact found in the bind() documentation.
而现在,你在体内推动的每个.link-in标签都会响应点击监听器。事实上,在bind()文档中可以找到关于默认使用on()而不是bind()的讨论。
#1
0
If that .link-in tag if dinamically inserted into the DOM after the page loads, the bind() method will fail: bind() attach listeners to elements it find when called. If you add a new elemenent after the bind call, it will not receive the listener binding.
如果.link-in标记在页面加载后以语法形式插入到DOM中,则bind()方法将失败:bind()将侦听器附加到调用时找到的元素。如果在绑定调用后添加新的元素,它将不会收到侦听器绑定。
In order to prevent that when dinamically inserting html code in a page, you should use the on() method:
为了防止在页面中插入html代码时,应该使用on()方法:
$('body').on('click' , '.link-in', function(e) {
e.preventDefault();
var pageIn = $(this).attr('href');
loadPage(pageIn);
return false;
});
And now, every .link-in tag you push dinamically inside body will respond to the click listener. A discussion on using on() by default instead of bind() is in fact found in the bind() documentation.
而现在,你在体内推动的每个.link-in标签都会响应点击监听器。事实上,在bind()文档中可以找到关于默认使用on()而不是bind()的讨论。