Javascript 动态替换div容器中的数据
在DIV容器中动态加载数据的最佳方法是什么 假设我在一页上有许多图片。单击图片会在屏幕上打开一个DIV容器(无页面刷新),其中包含该图片的所有注释 目前,我正在发送一个AJAX请求,以便从DB加载注释,并在另一个页面中回送它们。然后,我用jQuery将该数据附加到DIV容器中。单击另一个图像将清除容器,并根据新的AJAX请求加载新数据 在我看来,这可能不是最有效的方法,因为如果用户来回点击页面上的图像,那么每次都必须重新加载数据 我发现每次选择新图片时,facebook都会更改URL。因此,他们可能使用URL中的ID来加载数据,而不发送AJAX请求Javascript 动态替换div容器中的数据,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,在DIV容器中动态加载数据的最佳方法是什么 假设我在一页上有许多图片。单击图片会在屏幕上打开一个DIV容器(无页面刷新),其中包含该图片的所有注释 目前,我正在发送一个AJAX请求,以便从DB加载注释,并在另一个页面中回送它们。然后,我用jQuery将该数据附加到DIV容器中。单击另一个图像将清除容器,并根据新的AJAX请求加载新数据 在我看来,这可能不是最有效的方法,因为如果用户来回点击页面上的图像,那么每次都必须重新加载数据 我发现每次选择新图片时,facebook都会更改URL。因此,他们
可能是一旦加载数据并选择了另一个图像,最好只隐藏页面上的旧图像注释,而不是丢弃它们?因此,如果再次单击图像,则不需要重新加载其数据 如果您在某个div中重新指定
header('Content-Type: '.get_img_type($data));
header('Content-Length: '.strlen($data));
header("Pragma: public");
$expires = 60*60*24*14;
header("Cache-Control: maxage=".$expires);
header("Last-Modified: Mon, 26 Jul 1984 05:00:00 GMT");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
$('#divid').html('header('Content-Type: '.get_img_type($data));
header('Content-Length: '.strlen($data));
header("Pragma: public");
$expires = 60*60*24*14;
header("Cache-Control: maxage=".$expires);
header("Last-Modified: Mon, 26 Jul 1984 05:00:00 GMT");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
如果您在某个div中重新指定
header('Content-Type: '.get_img_type($data));
header('Content-Length: '.strlen($data));
header("Pragma: public");
$expires = 60*60*24*14;
header("Cache-Control: maxage=".$expires);
header("Last-Modified: Mon, 26 Jul 1984 05:00:00 GMT");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
$('#divid').html('header('Content-Type: '.get_img_type($data));
header('Content-Length: '.strlen($data));
header("Pragma: public");
$expires = 60*60*24*14;
header("Cache-Control: maxage=".$expires);
header("Last-Modified: Mon, 26 Jul 1984 05:00:00 GMT");
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');
可能会有很多帮助。您可以在javascript对象中添加get地址,并在每次发出ajax请求时将加载的内容映射到该地址。
然后,当用户单击图像时,不是发送ajax请求,而是首先检查对象中是否有该请求以从那里加载,如果对象中没有地址,则可以继续发送ajax请求并将其结果保存到我提到的对象。
可以在javascript对象中添加get地址,并在每次发出ajax请求时将加载的内容映射到它。
然后,当用户单击图像时,不是发送ajax请求,而是首先检查对象中是否有从那里加载的请求,如果对象中没有地址,则可以继续发送ajax请求,并将其结果保存到我提到的对象。
以下是如何避免对同一图像频繁/重复进行ajax请求的方法 假设您有一个函数可以在div中绘制注释,以接收来自服务器的响应,例如:
function displayComments(resp) {
//Do stuff
}
让我们假设对图像单击的ajax调用如下所示
//We're probably better of using the wrapper (class/ID) of images to
//delegate events since setting/unsetting data may cause problems
//if we'd used bind on images directly
$("imagesContainerSelector").on("click", "img", function() {
var $this = $(this);
//See if comments have already been fetched previously.
//If yes, we'd have stored them as that image's data and
//we'd now simply use that data to display the comments.
if ( $this.data("comments") ) {
displayComments($this.data("comments"));
} else {
//We've not fetched comments for this image yet, so, fetch it
$.ajax({
url: '...',
data: '...'
//more options
}).done(function(data) {
//When done, display comments
displayComments(data);
//Set the resp as data for later use
$this.data("comments", data);
});
}
});
这里介绍了如何避免对同一图像频繁/重复进行ajax请求 假设您有一个函数可以在div中绘制注释,以接收来自服务器的响应,例如:
function displayComments(resp) {
//Do stuff
}
让我们假设对图像单击的ajax调用如下所示
//We're probably better of using the wrapper (class/ID) of images to
//delegate events since setting/unsetting data may cause problems
//if we'd used bind on images directly
$("imagesContainerSelector").on("click", "img", function() {
var $this = $(this);
//See if comments have already been fetched previously.
//If yes, we'd have stored them as that image's data and
//we'd now simply use that data to display the comments.
if ( $this.data("comments") ) {
displayComments($this.data("comments"));
} else {
//We've not fetched comments for this image yet, so, fetch it
$.ajax({
url: '...',
data: '...'
//more options
}).done(function(data) {
//When done, display comments
displayComments(data);
//Set the resp as data for later use
$this.data("comments", data);
});
}
});
最好一次加载所有数据并隐藏不必要的数据。并在您需要的某个事件上显示这些数据。最好一次加载所有数据并隐藏不必要的数据。并在您需要的某个事件上显示这些数据。谢谢,这看起来是一个很好的方法。您将如何存储传入的数据?二维阵列?data()函数的外观如何?存储来自服务器的数据,或者存储任何您想要存储的数据。是一个内置的jQuery方法来存储任意数据。谢谢你。.data()函数必须附加到div元素吗?它将把我所有的注释数据存储在该元素的“data-x”属性中,这在DOM中是不可见的?性能呢?这种方法比进行重复的ajax调用快吗?是的,
data-*
通过设置。data()
将存储在内存中,在DOM中不可见。当然,这比无缘无故地攻击服务器要快得多!谢谢,看起来是个好办法。您将如何存储传入的数据?二维阵列?data()函数的外观如何?存储来自服务器的数据,或者存储任何您想要存储的数据。是一个内置的jQuery方法来存储任意数据。谢谢你。.data()函数必须附加到div元素吗?它将把我所有的注释数据存储在该元素的“data-x”属性中,这在DOM中是不可见的?性能呢?这种方法比进行重复的ajax调用快吗?是的,data-*
通过设置。data()
将存储在内存中,在DOM中不可见。当然,这比无缘无故地攻击服务器要快得多!