Table of Contents
回复内容:
先说明下实现
再说下效果
SEO
Home Backend Development PHP Tutorial javascript - 用ajax换一个屏幕的数据

javascript - 用ajax换一个屏幕的数据

Jun 06, 2016 pm 08:37 PM
ajax javascript php seo Front-end development

为了达到更好的用户体验,使用ajax来做数据的切换。假设,一页显示100个产品,产品有图片,有标题以及其他属性。用户点击一些条件,ajax重新加载一页数据。如何操作dom,让加载的过程看起来更柔和?这个词不知道用的对不对,最简单粗暴的就是,数据dom全清掉,通过ajax获取的json重新组织dom替换过去。更好的做法是怎样呢?

这个问题,大家的回答,已经很收益了。我在加大些范围,如何在用户体验与SEO之间平衡呢?我需要搜索引擎收录我的数据,但是这种AJAX的方式,搜索引擎又不认识?

回复内容:

为了达到更好的用户体验,使用ajax来做数据的切换。假设,一页显示100个产品,产品有图片,有标题以及其他属性。用户点击一些条件,ajax重新加载一页数据。如何操作dom,让加载的过程看起来更柔和?这个词不知道用的对不对,最简单粗暴的就是,数据dom全清掉,通过ajax获取的json重新组织dom替换过去。更好的做法是怎样呢?

这个问题,大家的回答,已经很收益了。我在加大些范围,如何在用户体验与SEO之间平衡呢?我需要搜索引擎收录我的数据,但是这种AJAX的方式,搜索引擎又不认识?

先说明点,清除dom和页面加载自然没有必然联系,不能说我操作了dom,我页面加载就不友好了。页面的友好性是通过交互实现。当然了,频繁大量的操作dom会带来性能问题,会延长页面的加载时间。

先说明下实现

要是我来做的话,会使用模板引擎如handlebars,拿到数据后渲染成html插入到dom中。如果使用了MVVM框架,只需要改变下model就可以了,但是大部分的框架还是如题主所说的粗暴方式处理的,angularjs除外。所以楼主不要纠结了,就得简单粗暴的干,关键是怎么粗暴的干,让人感觉不出来。

再说下效果

首先,在不同页切换时,加个loading的效果。
其次,使用模板引擎管理你的html
如果所有的产品不是一次性展示在页面上,使用图片懒加载
请求过的数据要缓存下来,避免再次请求

SEO

如果要做SEO的话,这种方式是不可行的,因为你的数据都是通过js去渲染的,搜索引擎爬取你的链接时,页面内容都是空的,那怎么收录你的产品链接?
刚咨询了我们的SEO工程师,国内搜索引擎还是以百度为主的,暂时还没办法执行js。为了SEO还是采用后端渲染的方式吧。
除非你们产品有这么吊,那就像它这样
javascript - 用ajax换一个屏幕的数据

你提的这个问题是不是想问通过什么样的交互才能感觉起来更好?其实这个关键靠你自己的想象了
可以这样,点击执行ajax的时候,把要替换的dom先用渐隐效果隐藏掉,然后弹出一个loading的提示,成功获取数据后,替换数据,然后loadding消失,dom显示
获取数据有时候会很快,所以,在替换完数据后,延迟一下再进行交互,大约300ms就够了

"柔和"主要靠的是交互细节,比如加载过程中的视觉反馈(进度条/菊花),加载过程中的交互(正在加载的时候做了其他操作怎么办?是mask蒙住,是中止之前的加载,还是操作无效并有视觉反馈“加载中请勿操作”?),以及加载时间过长时的反馈(类似gmail“加载比预期慢,是否切换到普通html版本” “仍在进行/重试”)等等

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

See all articles