简体   繁体   English

Location.reload更优雅/高效的解决方案

[英]Location.reload more elegant/efficient solution

Situation: 情况:

I have an html page with a PHP function on it. 我有一个带有PHP函数的html页面。 The function echoes the elements of an array in a foreach loop. 该函数在foreach循环中回显数组的元素。 The data of the array is used in each individual echo. 数组的数据用于每个单独的回波中。

foreach ($DB->query($sql) as $v) {
    echo $v['specificData'];
}

All new elements are added to the array with ajax. 所有新元素都使用ajax添加到数组中。

Problem: 问题:

I have been using location.reload to refresh the page each time a new element is added so it show ups right away. 每次添加新元素时,我一直在使用location.reload刷新页面,以便立即显示它。 The problem is that that solution isn't elegant/efficient at all. 问题在于该解决方案根本不够优雅/高效。

Question: 题:

What are my other options? 我还有其他选择吗? The easiest, the better. 最简单,更好。

Appreciate any help and advice ;) 感谢任何帮助和建议;)

PS: I already thought about jquery.append() but I'm hoping someone has an easier idea xD PS:我已经考虑过jquery.append()但我希望有人对xD有一个更简单的想法

Refresh just the part of the page that needs refreshing. 仅刷新页面中需要刷新的部分。 You can make it smooth by doing something like this in jQuery upon successful AJAX callback: 成功的AJAX回调后,您可以通过在jQuery中执行以下操作来使其变得平滑:

$(element).fadeOut(function(){
    $(element).html(ajax_response_here).fadeIn(); // .html() or .append() whichever makes more sense
});

If you already using ajax to send the data back to the server its should be hard to use it to update the dom as well. 如果您已经使用ajax将数据发送回服务器,则也很难使用它来更新dom。 If you want an efficient way then probably this is the way to go. 如果您想要一种有效的方法,那么可能就是这样。 If you want to avoid jQ for updating the site, you can simply write a basic js function. 如果要避免使用jQ来更新站点,则只需编写一个基本的js函数。

function appendTo(parent, content) {
    parent.innerHTML += "<p>"+content+"</p>";
}

or 要么

function appendToById(parentId, content) {
    appendTo(document.getElementById(parentId), content);
}

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

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