简体   繁体   English

Ajax 调用后更新 DOM 的最佳实践

[英]Best practise for update DOM after Ajax call

I want to know what's the best practise for updating DOM after an Ajax call.我想知道在 Ajax 调用后更新 DOM 的最佳实践是什么。

For example, imagine we have a list of users and we can add a user with a form which make an Ajax call to insert the user.例如,假设我们有一个用户列表,我们可以使用一个表单添加一个用户,该表单进行 Ajax 调用以插入用户。 After the form is submitted and the user added in database, the DOM is edited to add HTML without refreshing the page (with the Ajax success event).提交表单并将用户添加到数据库后,编辑 DOM 以添加 HTML 而不刷新页面(使用 Ajax 成功事件)。

I see two possibility to make this :我看到了两种可能性:

  1. Make an Ajax call who add the user in db and return all the DOM structure with html tag, etc.进行 Ajax 调用,将用户添加到 db 并返回所有带有 html 标签的 DOM 结构等。

  2. Make an Ajax who add the user in db and return all the data of an user and create the DOM element in Javascript制作一个 Ajax,在 db 中添加用户并返回用户的所有数据并在 Javascript 中创建 DOM 元素

What'is the best way to do it (or another way) ?什么是最好的方法(或其他方法)?

You can add elements to the DOM with document.createElement() .您可以使用document.createElement()向 DOM 添加元素。 Then use the innerHTML property to add content to the element.然后使用innerHTML属性向元素添加内容。 Finally append the element to another with .appendChild() .最后使用.appendChild()将元素附加到另一个元素。

There is no better way between the two options.这两个选项之间没有更好的方法。 You can either prepare your HTML structure in the backend and import the written HTML and directly append it with JavaScript or create the elements frontend in JavaScript.您可以在后端准备您的 HTML 结构并导入编写的 HTML 并直接使用 JavaScript 附加它,或者在 JavaScript 中创建元素前端。

Here are the main advantages to using the latter:以下是使用后者的主要优点:

  • it's easier to debug JavaScript than PHP (PHP files targetted by AJAX calls are quite hard to debug and to maintain).调试 JavaScript 比 PHP 更容易(以 AJAX 调用为目标的 PHP 文件很难调试和维护)。

  • returning an object with AJAX instead of an HTML string is easier to use and more maintainable.使用 AJAX 而不是 HTML 字符串返回对象更易于使用且更易于维护。 You can have an API to handle the data.您可以使用 API 来处理数据。 PHP only returns a JSON object. PHP 只返回一个 JSON 对象。

  • if JavaScript handles the creation of elements, you can save those in variables.如果 JavaScript 处理元素的创建,您可以将它们保存在变量中。 Sometimes, I find it useful to save an HTMLElement in a JavaScript variable so I can later access it to change its properties without having to go through selectors ( querySelector() and others).有时,我发现将HTMLElement保存在 JavaScript 变量中很有用,这样我以后就可以访问它以更改其属性,而无需通过选择器( querySelector()和其他)。

I would go with the second option我会选择第二个选项

  • Make an Ajax who add the user in db and return all the data of an user and create the DOM element in Javascript制作一个 Ajax,在 db 中添加用户并返回用户的所有数据并在 Javascript 中创建 DOM 元素

Breaking it down like so to improve on readability and maintenance像这样分解它以提高可读性和维护性

  • Add action to AJAX that is linked to a function on the backend, (ie PHP, Python) that does the DB update and more.向 AJAX 添加操作,该操作链接到后端的函数(即 PHP、Python),该函数执行数据库更新等。

  • Create the Element structures in Javascript (You can maintenance uniformity with other structures on your page).在 Javascript 中创建元素结构(您可以与页面上的其他结构保持统一)。 And wrap it in a function.并将其包装在一个函数中。

     document.body.onload = addElement; function addElement ($newuser) { // create a new div element that would contain user record var newDiv = document.createElement("div"); // and give it some content var newContent = document.createTextNode($newuser); // add the text node to the newly created div newDiv.appendChild(newContent); // add the newly created element and its content into the DOM var currentDiv = document.getElementById("user-container"); document.body.insertBefore(newDiv, currentDiv); }

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

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