<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ajax &#8211; 萧邦的博客</title>
	<atom:link href="https://www.zlhlab.top/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.zlhlab.top</link>
	<description>If not me who, If not now when</description>
	<lastBuildDate>Tue, 15 Aug 2017 02:02:23 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.6</generator>
	<item>
		<title>php+ajax+JSON API实现单页面内标签切换</title>
		<link>https://www.zlhlab.top/phpajaxjson-api%e5%ae%9e%e7%8e%b0%e5%8d%95%e9%a1%b5%e9%9d%a2%e5%86%85%e6%a0%87%e7%ad%be%e5%88%87%e6%8d%a2/</link>
		
		<dc:creator><![CDATA[萧邦]]></dc:creator>
		<pubDate>Tue, 25 Jul 2017 09:27:55 +0000</pubDate>
				<category><![CDATA[技术]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">https://www.zlhlab.top/?p=175</guid>

					<description><![CDATA[公司制作新公司的官网，采用wordpress来进行站点搭建。其中首页的案例展示模块，设计如下图： 如上图，需要<div class="more-link">
				 <a href="https://www.zlhlab.top/phpajaxjson-api%e5%ae%9e%e7%8e%b0%e5%8d%95%e9%a1%b5%e9%9d%a2%e5%86%85%e6%a0%87%e7%ad%be%e5%88%87%e6%8d%a2/" class="link-btn theme-btn"><span>Read More </span> <i class="fa fa-caret-right"></i></a>
			</div>]]></description>
										<content:encoded><![CDATA[<p>公司制作新公司的官网，采用wordpress来进行站点搭建。其中首页的案例展示模块，设计如下图：</p>
<figure><img fetchpriority="high" decoding="async" class="aligncenter size-large wp-image-176" src="https://www.zlhlab.top/wp-content/uploads/2017/07/case-1024x482.jpg" alt="" width="1024" height="482" srcset="https://www.zlhlab.top/wp-content/uploads/2017/07/case-1024x482.jpg 1024w, https://www.zlhlab.top/wp-content/uploads/2017/07/case-300x141.jpg 300w, https://www.zlhlab.top/wp-content/uploads/2017/07/case-768x361.jpg 768w, https://www.zlhlab.top/wp-content/uploads/2017/07/case.jpg 1245w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>如上图，需要在首页进行案例的切换。</p>
<p>实现功能想到有两种解决方案：<br />
<strong>方案一</strong>：除index.php文件外，创建三个分类文件category-h5.php、category-media.php、category-design.php，点击标签，跳转到对应的分类页面。基本可以实现标签分页功能。更加精细点，可以缓存当前页面位置，在新的页面设置页面滚动至缓存位置。本方案基本可以实现功能。唯一缺点：页面过渡时根据网络情况会出现闪烁、多3个php文件。</p>
<p><strong>方案二</strong>：单页面index.php，安装JSON API的插件，采用ajax的形式，动态获取分类内容，并动态更新页面内容。</p>
<p>当前采用<strong>方案二</strong>进行技术实现。</p>
<p>1、在wordpress 管理后台创建分类标签，如下图：</p>
<figure><img decoding="async" class="aligncenter size-large wp-image-178" src="https://www.zlhlab.top/wp-content/uploads/2017/07/3BE98391-7D1E-464D-9C29-83ECF442D448-1024x253.png" alt="" width="1024" height="253" srcset="https://www.zlhlab.top/wp-content/uploads/2017/07/3BE98391-7D1E-464D-9C29-83ECF442D448-1024x253.png 1024w, https://www.zlhlab.top/wp-content/uploads/2017/07/3BE98391-7D1E-464D-9C29-83ECF442D448-300x74.png 300w, https://www.zlhlab.top/wp-content/uploads/2017/07/3BE98391-7D1E-464D-9C29-83ECF442D448-768x189.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>2、安装<strong>JSON API</strong>插件并启用</p>
<figure><img decoding="async" class="aligncenter size-large wp-image-182" src="https://www.zlhlab.top/wp-content/uploads/2017/07/4FB71529-36E1-4CA7-A7EF-633CE10769E4-1024x84.png" alt="" width="1024" height="84" srcset="https://www.zlhlab.top/wp-content/uploads/2017/07/4FB71529-36E1-4CA7-A7EF-633CE10769E4-1024x84.png 1024w, https://www.zlhlab.top/wp-content/uploads/2017/07/4FB71529-36E1-4CA7-A7EF-633CE10769E4-300x25.png 300w, https://www.zlhlab.top/wp-content/uploads/2017/07/4FB71529-36E1-4CA7-A7EF-633CE10769E4-768x63.png 768w, https://www.zlhlab.top/wp-content/uploads/2017/07/4FB71529-36E1-4CA7-A7EF-633CE10769E4.png 1944w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>3、添加分类文章，测试JSON API</p>
<pre><code>
var clickedItem = 0;
var $items = $('#categoryMenu').find('a');
var slugList = ['case', 'h5', 'media', 'design'];
function clickItem(index) {
     /*更新按钮选项的状态*/
     $($items[clickedItem]).removeClass('hover');
     $($items[index]).addClass('hover');
     clickedItem = index;

     /*请求API，获取分类的内容*/
     var url = '/yanrui/?json=get_category_posts&slug=' + slugList[index] +'&count=6';
     $.ajax({
         type: "GET",
         url: url,
         dataType: "json",
         jsonp: "callback",
         success: function (data) {
             console.log(data);
             var res = eval(data);
             if (res.status == 'ok') {
                  // todo 解析文章内容
             }
         },
         error:function(msg){
             msg = eval('(msg)');
             console.log(msg);
         }
     });
}
</code></pre>
<p>测试结果如下图，即正常接口测试通过：</p>
<figure><img loading="lazy" decoding="async" src="https://www.zlhlab.top/wp-content/uploads/2017/07/2E03463D-3914-4C84-BFA3-DF2E0E282456-1024x725.png" alt="" width="1024" height="725" class="aligncenter size-large wp-image-186" srcset="https://www.zlhlab.top/wp-content/uploads/2017/07/2E03463D-3914-4C84-BFA3-DF2E0E282456-1024x725.png 1024w, https://www.zlhlab.top/wp-content/uploads/2017/07/2E03463D-3914-4C84-BFA3-DF2E0E282456-300x212.png 300w, https://www.zlhlab.top/wp-content/uploads/2017/07/2E03463D-3914-4C84-BFA3-DF2E0E282456-768x544.png 768w, https://www.zlhlab.top/wp-content/uploads/2017/07/2E03463D-3914-4C84-BFA3-DF2E0E282456.png 1590w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
<p>4、ajax获取结果，动态更新模块内容</p>
<figure><img loading="lazy" decoding="async" src="https://www.zlhlab.top/wp-content/uploads/2017/07/4973DB30-6006-4C9B-BFC8-E078F60EF7A2-1024x255.png" alt="" width="1024" height="255" class="aligncenter size-large wp-image-205" srcset="https://www.zlhlab.top/wp-content/uploads/2017/07/4973DB30-6006-4C9B-BFC8-E078F60EF7A2-1024x255.png 1024w, https://www.zlhlab.top/wp-content/uploads/2017/07/4973DB30-6006-4C9B-BFC8-E078F60EF7A2-300x75.png 300w, https://www.zlhlab.top/wp-content/uploads/2017/07/4973DB30-6006-4C9B-BFC8-E078F60EF7A2-768x191.png 768w, https://www.zlhlab.top/wp-content/uploads/2017/07/4973DB30-6006-4C9B-BFC8-E078F60EF7A2.png 1880w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>
<p>5、完成单页面动态切换。</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
