<?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>裁纸刀下 &#187; jquery</title>
	<atom:link href="http://ofcss.com/tags/jquery/feed" rel="self" type="application/rss+xml" />
	<link>http://ofcss.com</link>
	<description>独立 自由 诚信 宽容 责任 平常心</description>
	<lastBuildDate>Fri, 20 Apr 2012 04:08:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>借助jQuery和Google分析服务的事件追踪深入了解用户行为【译】</title>
		<link>http://ofcss.com/2010/10/25/understanding-user-behavior-google-analytics-event-tracking-jquer.html</link>
		<comments>http://ofcss.com/2010/10/25/understanding-user-behavior-google-analytics-event-tracking-jquer.html#comments</comments>
		<pubDate>Mon, 25 Oct 2010 12:09:41 +0000</pubDate>
		<dc:creator>小李刀刀</dc:creator>
				<category><![CDATA[所谓技术]]></category>
		<category><![CDATA[Google Analytics]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[交互研究]]></category>
		<category><![CDATA[用户行为]]></category>
		<category><![CDATA[翻译]]></category>

		<guid isPermaLink="false">http://ofcss.com/?p=990</guid>
		<description><![CDATA[作者:Brian Cray 原文:http://briancray.com/?p=1584 【作者简介】Brian Cray 居住在俄亥俄州的哥伦布市，是位于圣弗朗西斯科的Topsy公司的一名前端工程师。 本文主要介绍如何使用谷歌分析的高级事件跟踪能力，深入了解用户如何浏览你的网站、如何与你的网页进行交互。 =========== 以下为译文正文=============== Google分析服务的设置，对于访问者浏览行为和偏好的信息，仅记录和分析了他们的来路和被访问的页面。但这些信息就足够告诉你访问者在网页中的用户行为了吗？不够！这些只是帮助我们分析用户行为的最基础的信息。 我想请你根据你目前的Google分析的设定回答以下问题： 你的用户是通过主菜单还是通过其它方式来查找你站内的内容？ 你的用户在查看较长的页面时向下滚动和点击底部的链接吗？ 你的用户是点击文章的标题还是点击“查看详情”链接？ 你的用户真的会使用下拉菜单里的项目吗？还是根本没注意到下拉菜单？ 你的用户接受你建议的的相关文章吗？ 现在让我来帮助你回答其中的一部分问题 借助Google分析服务的事件追踪，可以跟踪在你网页上发生的任意事件——包括用户的行为。 在开始之前，有两个重要步骤需要先做： 把你的Google分析的跟踪代码改为异步跟踪的代码。 把jQuery代码移到页面的&#60;head&#62;标签里（我知道，这是违背优化标准的，但是如果你使用良好的缓存技术的话，后面的数据回报是绝对值得你这样做的）。 代码 把以下代码添加到你的网页的&#60;head&#62;标签中或者你当前的jQuery代码中（只有该代码在&#60;head&#62;标签中时才可以）。 $(function () { $('a').click(function () { // 通知分析服务存储事件 try { _gaq.push(['_trackEvent', thisel.parents('[id!=&#34;&#34;]:first').get(0).id, 'clicked', (thisel.text() &#124;&#124; thisel.children('img:first').attr('alt'))]); } catch (err) { } // pause to allow google script to run var date = [...]]]></description>
			<content:encoded><![CDATA[<p>作者:<a href="http://briancray.com/" rel="tag">Brian Cray</a></p>
<p>原文:<a href="http://briancray.com/?p=1584">http://briancray.com/?p=1584</a></p>
<p>【作者简介】Brian Cray 居住在俄亥俄州的哥伦布市，是位于圣弗朗西斯科的<a href="http://www.google.com/url?q=http%3A%2F%2Ftopsy.com%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNFHGPfLgD2N7e35m-6aqFDYAa1BQg">Topsy</a>公司的一名前端工程师。</p>
<p>本文主要介绍如何使用谷歌分析的高级事件跟踪能力，深入了解用户如何浏览你的网站、如何与你的网页进行交互。</p>
<p>  <span id="more-990"></span>
<p>=========== 以下为译文正文===============</p>
<p>Google分析服务的设置，对于访问者浏览行为和偏好的信息，仅记录和分析了他们的来路和被访问的页面。但这些信息就<strong>足够</strong>告诉你访问者<strong>在网页中</strong>的用户行为了吗？不够！这些只是帮助我们分析用户行为的最基础的信息。</p>
<h4>我想请你根据你目前的Google分析的设定回答以下问题：</h4>
<ul>
<li>你的用户是通过主菜单还是通过其它方式来查找你站内的内容？ </li>
<li>你的用户在查看较长的页面时向下滚动和点击底部的链接吗？ </li>
<li>你的用户是点击文章的标题还是点击“查看详情”链接？ </li>
<li>你的用户真的会使用下拉菜单里的项目吗？还是根本没注意到下拉菜单？ </li>
<li>你的用户接受你建议的的相关文章吗？ </li>
</ul>
<p align="center"><a href="http://file.wukangrui.com/attachments/2010/10/b01.png"><img title="b01" border="0" alt="b01" src="http://file.wukangrui.com/attachments/2010/10/b01_thumb.png" /></a></p>
<h4>现在让我来帮助你回答其中的一部分问题</h4>
<p>借助Google分析服务的<a href="http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html" rel="tag">事件追踪</a>，可以跟踪<strong>在你网页上</strong>发生的任意事件——包括用户的行为。</p>
<p>在开始之前，有两个<strong>重要步骤</strong>需要先做：</p>
<ul>
<li>把你的Google分析的跟踪代码改为<a href="http://www.google.com/url?q=http%3A%2F%2Fbriancray.com%2F2009%2F12%2F20%2Fasynchronous-tracking-google-analytics%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNHnPBbNLRha5QZAMMkJD0S7CLqg4w">异步跟踪</a>的代码。</li>
<li>把<a href="http://www.google.com/url?q=http%3A%2F%2Fjquery.com%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNGtx3hYIQpONgUoQvrnRm8YULAPpA">jQuery</a>代码移到页面的&lt;head&gt;标签里（我知道，这是违背<a href="http://www.google.com/url?q=http%3A%2F%2Fdeveloper.yahoo.com%2Fperformance%2Frules.html&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNG29Nlpb9nSQXhNjB5G0ZMVzCnnYA">优化标准</a>的，但是如果你使用<a href="http://www.google.com/url?q=http%3A%2F%2Fwww.mnot.net%2Fcache_docs%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNFnJ-X7s5Ux_3Kg822gi-DVc5X6vg">良好的缓存技术</a>的话，后面的数据回报是绝对值得你这样做的）。</li>
</ul>
<h4>代码</h4>
<p>把以下代码添加到你的网页的&lt;head&gt;标签中或者你当前的jQuery代码中（只有该代码在&lt;head&gt;标签中时才可以）。</p>
<pre class="code">$(<span style="color: blue">function </span>() {
    $(<span style="color: maroon">'a'</span>).click(<span style="color: blue">function </span>() {
    <span style="color: #006400">// 通知分析服务存储事件
        </span><span style="color: blue">try </span>{
            _gaq.push([<span style="color: maroon">'_trackEvent'</span>, thisel.parents(<span style="color: maroon">'[id!=&quot;&quot;]:first'</span>).get(0).id, <span style="color: maroon">'clicked'</span>, (thisel.text() || thisel.children(<span style="color: maroon">'img:first'</span>).attr(<span style="color: maroon">'alt'</span>))]);
        }
        <span style="color: blue">catch </span>(err) { }

        <span style="color: #006400">// pause to allow google script to run
        </span><span style="color: blue">var </span>date = <span style="color: blue">new </span>Date();
        <span style="color: blue">var </span>curDate = <span style="color: blue">null</span>;
        <span style="color: blue">do </span>{
            curDate = <span style="color: blue">new </span>Date();
        } <span style="color: blue">while </span>(curDate - date &lt; 300);
    });
});</pre>
<h4>添加代码后对事件按分类查看的截图</h4>
<p>在添加以上的代码后，可以看到网站的那些部分被点击得最多（基于元素的id属性或者父元素）。</p>
<p align="center"><a href="http://file.wukangrui.com/attachments/2010/10/b02.png"><img title="b02" border="0" alt="b02" src="http://file.wukangrui.com/attachments/2010/10/b02_thumb.png" /></a></p>
<h4>添加代码后对事件按标签查看的截图</h4>
<p>在添加以上的代码后，从左边的下拉菜单中选择某一个事件分类，可以看到被点击过的链接的标题，例如下图中能够看到的菜单项中的链接文字。</p>
<p align="center"><a href="http://file.wukangrui.com/attachments/2010/10/b03.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="b03" border="0" alt="b03" src="http://file.wukangrui.com/attachments/2010/10/b03_thumb.png" width="384" height="347" /></a></p>
<h4>通过事件追踪获得有关用户行为分析的更多信息</h4>
<p>前面所讲的，只是对这个强大技术的一点点入门介绍。借助用户行为分析方面的知识和恰当的jQuery技术，你还能以更丰富、更深入的方式使用它。例如：</p>
<ul>
<li>追踪用户的表单行为和鼠标行为。</li>
<li>追踪用户的操作和操作之间的时间。</li>
<li>改变设计，然后观察这些改变如何影响用户的行为（类似于A/B测试）。</li>
</ul>
<p><a href="http://www.google.com/url?q=http%3A%2F%2Fwww.fabianpimminger.com%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNGInfju6T5mo3B-vh31v-9z8zYJMw">Fabian Pimminge</a> 已经在 MooTools 上重写了这些代码，如果你使用的是 MoonTools 框架，可以参考<a href="http://www.google.com/url?q=http%3A%2F%2Fwww.fabianpimminger.com%2Fweb-development%2Ftracking-user-behaviour-with-google-analytics-event-tracking-and-mootools%2F&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNH5eKmcjT9OZjWLPCSVIUjC0x4m0A" rel="tag">这篇文章</a>。</p>
<h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2011-03-14 -- <a href="http://ofcss.com/2011/03/14/browser-rendering-optimizer-translation-extra.html" title="对《优化浏览器渲染》的补充【译】">对《优化浏览器渲染》的补充【译】</a></li><li>2011-03-10 -- <a href="http://ofcss.com/2011/03/10/css-border-tricks-with-collapsed-boxes-translation.html" title="CSS边框实现&ldquo;无图化&rdquo;设计【译】">CSS边框实现&ldquo;无图化&rdquo;设计【译】</a></li><li>2010-10-31 -- <a href="http://ofcss.com/2010/10/31/prevent-a-float-drop-in-ie6-translation.html" title="CSS技巧:如何避免IE6中的&quot;浮动下坠&quot;【译】">CSS技巧:如何避免IE6中的&quot;浮动下坠&quot;【译】</a></li><li>2010-10-22 -- <a href="http://ofcss.com/2010/10/22/the-value-of-html-validation-translation.html" title="HTML验证的价值探讨【译】">HTML验证的价值探讨【译】</a></li><li>2010-10-20 -- <a href="http://ofcss.com/2010/10/20/clearfix-reloaded-overflowhidden-demystified-translation.html" title="clearfix改良及overflow:hidden详解【译】">clearfix改良及overflow:hidden详解【译】</a></li><li>2009-07-08 -- <a href="http://ofcss.com/2009/07/08/asp-net-mvc-best-practices-part-4.html" title="ASP.NET MVC 最佳实践（四）">ASP.NET MVC 最佳实践（四）</a></li><li>2009-07-07 -- <a href="http://ofcss.com/2009/07/07/asp-net-mvc-best-practices-part-3.html" title="ASP.NET MVC 最佳实践（三）">ASP.NET MVC 最佳实践（三）</a></li><li>2009-07-01 -- <a href="http://ofcss.com/2009/07/01/asp-net-mvc-best-practices-part-2.html" title="ASP.NET MVC 最佳实践（二）">ASP.NET MVC 最佳实践（二）</a></li><li>2009-06-28 -- <a href="http://ofcss.com/2009/06/28/asp-net-mvc-best-practices-part-1.html" title="ASP.NET MVC 最佳实践（一）">ASP.NET MVC 最佳实践（一）</a></li><li>2009-06-22 -- <a href="http://ofcss.com/2009/06/22/whatever-hover-pseudo-class-without-javascript.html" title="Whatever:hover &#8211; 无需javascript让IE支持丰富伪类">Whatever:hover &#8211; 无需javascript让IE支持丰富伪类</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://ofcss.com/2010/10/25/understanding-user-behavior-google-analytics-event-tracking-jquer.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VS2008 中 JS IntelliSense出错的解决</title>
		<link>http://ofcss.com/2009/04/30/solution-for-js-intellisense-error.html</link>
		<comments>http://ofcss.com/2009/04/30/solution-for-js-intellisense-error.html#comments</comments>
		<pubDate>Thu, 30 Apr 2009 18:55:01 +0000</pubDate>
		<dc:creator>小李刀刀</dc:creator>
				<category><![CDATA[所谓技术]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://wukangrui.net/2009/04/30/solution-for-js-intellisense-error.html</guid>
		<description><![CDATA[JScript 智能提示(IntelliSense)是 Visual Studio 2008 的一个重要新特性，尤其是现在 Visual Studio 2008 还提供了 jQuery 智能提示的官方支持。遗憾的是在实际应用中经常遇到如图所示的错误提示，并且出现这样的错误以后，包括jQuery在内的智能提示统统失效。非常让人恼火。我最开始的时候仅从提示“Error updating JScript IntelliSense”猜测以为是VS把智能提示缓存起来，然后缓存文件出错。后来尝试过很多方式都无法解决。 后来偶然看到 jeffrey 写的 日志，才知道原来是由于加载的额外js文件缺乏智能提示文件（vsdoc.js）导致的。 jeffrey 有一个非常简单易行的解决建议，就是创建一个和出错文件对应的 –vsdoc.js 文件放到该文件相同的目录下。比如 ASP.NET MVC 里默认就为jQuery.min.js 提供一个 jQuery.min-vsdoc.js 文件， 同样的道理，如果我们在 Scripts 目录下有一个 myScript.js 文件， 那么我们只需要创建一个空的 myScript-vsdoc.js 文件放到 Scripts 目录下，就可以避免因为加载 myScript.js 文件导致的 JS 智能提示失效问题。 当然了，这只是权益之计，不过很有效，因为我们在编写js的时候，增加的js文件往往是自己写的脚本，也就无所谓有没有智能提示了，如果你真的需要智能提示，那么不妨写一个。 由这个问题引发出来一个问题，就是对于在网上发布共享JS库的人，是不是应该考虑完善你的作品，提供对应的&#160; -vsdoc.js 文件呢？ 相关日志2009-04-13 -- [翻译]ASP.NET MVC动手实验1-3：开发ASP.NET MVC应用2009-04-09 -- [翻译]ASP.NET MVC动手实验1-2：创建ASP.NET [...]]]></description>
			<content:encoded><![CDATA[<p>JScript 智能提示(IntelliSense)是 Visual Studio 2008 的一个重要新特性，尤其是现在 Visual Studio 2008 还提供了 jQuery 智能提示的官方支持。遗憾的是在实际应用中经常遇到如图所示的错误提示，并且出现这样的错误以后，包括jQuery在内的智能提示统统失效。非常让人恼火。我最开始的时候仅从提示“Error updating JScript IntelliSense”猜测以为是VS把智能提示缓存起来，然后缓存文件出错。后来尝试过很多方式都无法解决。</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" alt="JScript IntelliSense Error" src="http://blog.darkthread.net/photos/darkthread/images/4566/original.aspx" /></p>
<p> <span id="more-561"></span>
<p>后来偶然看到 <a href="http://blog.darkthread.net/blogs/darkthreadtw/" rel="nofollow" target="_blank">jeffrey</a> 写的 <a href="http://blog.darkthread.net/blogs/darkthreadtw/archive/2009/04/22/js-intellisense-error.aspx" target="_blank">日志</a>，才知道原来是由于加载的额外js文件缺乏智能提示文件（vsdoc.js）导致的。</p>
<p>jeffrey 有一个非常简单易行的解决建议，就是创建一个和出错文件对应的 –vsdoc.js 文件放到该文件相同的目录下。比如 ASP.NET MVC 里默认就为jQuery.min.js 提供一个 jQuery.min-vsdoc.js 文件， 同样的道理，如果我们在 Scripts 目录下有一个 myScript.js 文件， 那么我们只需要创建一个空的 myScript-vsdoc.js 文件放到 Scripts 目录下，就可以避免因为加载 myScript.js 文件导致的 JS 智能提示失效问题。</p>
<p>当然了，这只是权益之计，不过很有效，因为我们在编写js的时候，增加的js文件往往是自己写的脚本，也就无所谓有没有智能提示了，如果你真的需要智能提示，那么不妨写一个。</p>
<p>由这个问题引发出来一个问题，就是对于在网上发布共享JS库的人，是不是应该考虑完善你的作品，提供对应的&#160; -vsdoc.js 文件呢？</p>
<h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2009-04-13 -- <a href="http://ofcss.com/2009/04/13/aspnetmvc-hands-on-labs-develop-application.html" title="[翻译]ASP.NET MVC动手实验1-3：开发ASP.NET MVC应用">[翻译]ASP.NET MVC动手实验1-3：开发ASP.NET MVC应用</a></li><li>2009-04-09 -- <a href="http://ofcss.com/2009/04/09/aspnetmvc-hands-on-labs-create-application.html" title="[翻译]ASP.NET MVC动手实验1-2：创建ASP.NET MVC应用">[翻译]ASP.NET MVC动手实验1-2：创建ASP.NET MVC应用</a></li><li>2009-04-09 -- <a href="http://ofcss.com/2009/04/09/aspnetmvc-hands-on-labs-introduction.html" title="[翻译]ASP.NET MVC动手系列1-1：ASP.NET MVC概述">[翻译]ASP.NET MVC动手系列1-1：ASP.NET MVC概述</a></li><li>2010-10-25 -- <a href="http://ofcss.com/2010/10/25/understanding-user-behavior-google-analytics-event-tracking-jquer.html" title="借助jQuery和Google分析服务的事件追踪深入了解用户行为【译】">借助jQuery和Google分析服务的事件追踪深入了解用户行为【译】</a></li><li>2010-04-14 -- <a href="http://ofcss.com/2010/04/14/vs2010-rtm-avaliable-on-msdn.html" title="Visual Studio 2010 Ultimate RTM 版开放MSDN下载">Visual Studio 2010 Ultimate RTM 版开放MSDN下载</a></li><li>2010-04-04 -- <a href="http://ofcss.com/2010/04/04/vspaste-customization-with-net-reflector.html" title="VSPaste 的持续改造">VSPaste 的持续改造</a></li><li>2009-07-08 -- <a href="http://ofcss.com/2009/07/08/asp-net-mvc-best-practices-part-4.html" title="ASP.NET MVC 最佳实践（四）">ASP.NET MVC 最佳实践（四）</a></li><li>2009-07-07 -- <a href="http://ofcss.com/2009/07/07/asp-net-mvc-best-practices-part-3.html" title="ASP.NET MVC 最佳实践（三）">ASP.NET MVC 最佳实践（三）</a></li><li>2009-07-01 -- <a href="http://ofcss.com/2009/07/01/asp-net-mvc-best-practices-part-2.html" title="ASP.NET MVC 最佳实践（二）">ASP.NET MVC 最佳实践（二）</a></li><li>2009-06-28 -- <a href="http://ofcss.com/2009/06/28/asp-net-mvc-best-practices-part-1.html" title="ASP.NET MVC 最佳实践（一）">ASP.NET MVC 最佳实践（一）</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://ofcss.com/2009/04/30/solution-for-js-intellisense-error.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>简单、通用的JQuery Tab实现</title>
		<link>http://ofcss.com/2009/04/13/portable-interchangeable-jquery-based-tabs.html</link>
		<comments>http://ofcss.com/2009/04/13/portable-interchangeable-jquery-based-tabs.html#comments</comments>
		<pubDate>Mon, 13 Apr 2009 03:35:34 +0000</pubDate>
		<dc:creator>小李刀刀</dc:creator>
				<category><![CDATA[所谓技术]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[代码]]></category>
		<category><![CDATA[网页]]></category>

		<guid isPermaLink="false">http://wukangrui.net/2009/04/13/portable-interchangeable-jquery-based-tabs.html</guid>
		<description><![CDATA[网页上的空间是寸土寸金，虽然显示器的分辨率越来越大，可是直到今天，网页设计中仍然是以至少1024&#215;768 像素的支持为主，也就是说，每一屏页面只有区区 955&#215;600像素 的安全尺寸可以用而已。于是，为了在有限的空间里容纳更多的内容，滑动门式的标签切换（Tabs）方式越来越受欢迎。通过滑动门技术，可以在同一块页面区域内放置数倍的内容。根据用户的选择来决定显示哪一部分。最近我在实际应用中，逐步完善出一种基于 jQuery，但是比 jQuery UI Tabs 插件更小巧也更通用的简单 Tabs 实现。 最早的滑动门的技术，一般都是结合 onclick 或者 onmouseover 事件传递一个参数给 JS 函数，根据传递的参数来决定显示哪一个标签。比如： function showTabs(n) { var tabsNumber = 3; for (i = 0; i &#60; tabsNumber; i++) { if (i == n) { document.getElementById("tabPanel-" + i).style.display = "block"; } else { document.getElementById("tabPanel-" + i).style.display = "none"; } } } [...]]]></description>
			<content:encoded><![CDATA[<p>网页上的空间是寸土寸金，虽然显示器的分辨率越来越大，可是直到今天，网页设计中仍然是以至少1024&#215;768 像素的支持为主，也就是说，每一屏页面只有区区 955&#215;600像素 的安全尺寸可以用而已。于是，为了在有限的空间里容纳更多的内容，滑动门式的标签切换（Tabs）方式越来越受欢迎。通过滑动门技术，可以在同一块页面区域内放置数倍的内容。根据用户的选择来决定显示哪一部分。最近我在实际应用中，逐步完善出一种基于 <a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a>，但是比 <a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a> UI Tabs 插件更小巧也更通用的简单 Tabs 实现。</p>
<p><span id="more-487"></span>最早的滑动门的技术，一般都是结合 onclick 或者 onmouseover 事件传递一个参数给 JS 函数，根据传递的参数来决定显示哪一个标签。比如：</p>
<pre class="code" style="width: 99.39%; height: 213px;"><span style="color: blue">function </span>showTabs(n) {
  <span style="color: blue">var </span>tabsNumber = 3;
  <span style="color: blue">for </span>(i = 0; i &lt; tabsNumber; i++) {
      <span style="color: blue">if </span>(i == n) {
          document.getElementById(<span style="color: #a31515">"tabPanel-" </span>+ i).style.display = <span style="color: #a31515">"block"</span>;
      } <span style="color: blue">else </span>{
          document.getElementById(<span style="color: #a31515">"tabPanel-" </span>+ i).style.display = <span style="color: #a31515">"none"</span>;
      }
  }
}</pre>
<p>加入有这样的一个函数，就可以在tab的标题按钮中设置 onclick = “showTabs(1)”来设置第二块内容显示，而其它块隐藏。</p>
<p>这种方式最大的缺点是：</p>
<ol>
<li>HTML代码和JS代码混合；</li>
<li>可扩展性差；</li>
</ol>
<p>尽管可以通过window.onload绑定事件等方式来是实现JS代码从HTML中分离，也可以把函数改得更复杂以实现通用性。但是总的来说，还是很难做到一处定义到处引用。</p>
<p>后来随着各种JS类库的出现，更强大的Tabs出现了，最出名的就是jQuery UI中的tabs插件。一旦加载了jQuery框架和jQuery UI插件，那么要在页面中实现Tabs，就变得简单了许多。首先我们的页面中的Tabs代码这样写：</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="tabs"&gt;
    &lt;</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="#panel-1"&gt;</span>标签一<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="#panel-2"&gt;</span>标签二<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="panel-1"&gt;</span>区域一<span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="panel-2"&gt;</span>区域二<span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<p>注意：这里的代码非常干净，不含任何的JS代码或者与文档结构无关的定义。然后，在head区域，或者在页面任何地方增加一段js代码：</p>
<pre class="code">$(<span style="color: blue">function</span>() {
    $(<span style="color: #a31515">".tabs"</span>).tabs();
});</pre>
<p>就实现了Tabs功能，这行JS代码执行后，上面的HTML代码就会变成：</p>
<pre class="code" style="width: 99.64%; height: 295px;"><span style="color: blue">&lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="tabs"&gt;
    &lt;</span><span style="color: #a31515">ul </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-nav" </span><span style="color: red">jquery1239647486215</span><span style="color: blue">="2"&gt;
        &lt;</span><span style="color: #a31515">li </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-selected"&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="#panel-1" </span><span style="color: red">jquery1239647486215</span><span style="color: blue">="8"&gt;</span>标签一<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="#panel-2" </span><span style="color: red">jquery1239647486215</span><span style="color: blue">="9"&gt;</span>标签二<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
        </span>&gt;<span style="color: blue">&lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="panel-1" </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-panel" </span><span style="color: red">jquery1239647486215</span><span style="color: blue">="4"&gt;
        </span>区域一
    <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">id</span><span style="color: blue">="panel-2" </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-panel ui-tabs-hide" </span><span style="color: red">jquery1239647486215</span><span style="color: blue">="5"&gt;
        </span>区域二
    <span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<p>结合我们自己编写的css，或者jQuery UI 自带的CSS，就可以实现滑动门效果。并且，由于jQuery的强大，我们可以在页面中放置多组滑动门，然后一次性设定。</p>
<p>要说明的是，这个地方由于只启用了jQuery UI 中的 Tabs 插件，因此生成的代码还是比较干净的，只增加了 ui-tabs-xxxx 这几个相关的css类。如果你同时包含了jQuery UI的其它插件，那么即使不启用，也会添加一堆的css定义。而且，<a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a> UI Tabs 还提供了非常强大的控制功能，你可以动态地添加 tab，可以随意更改激活事件，可以定义切换效果，还可以设置默认激活状态和禁用等。</p>
<p>但是我在实际应用中遇到了一些问题，除了 <a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a> UI 自带的js脚本很大，css不符合实际应用需求外，还有一个最大的问题，你可能已经注意到了，在作为导航的标签定义中，每个标签对应哪一个区域是用链接目标来定义的。比如 &lt;a href=”#panel-1&#8243;&gt;标签一&lt;/a&gt;和&lt;div id=”panel-1&#8243;&gt;区域一&lt;/div&gt;对应，如果你的标签和区域没有对应起来，绑定tabs()就不起作用了。</p>
<p>而且，这种方式来带来另一个麻烦，就是当我们需要给标签加上链接的时候，没办法加。即使你将标签的激活事件设置为onmouseover而不是onclick，链接也不能实现，因为链接用于指定目标了。这种需求在我们的实际应用中并不是不存在的。比如：</p>
<p><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb10.png" border="0" alt="image" /><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb11.png" border="0" alt="image" /></p>
<p>这两个图片中的tabs标签，都要添加到对应的新闻类别或者论坛板块的链接。这时候 <a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a> UI的默认绑定就带来了麻烦。</p>
<p>其实分析一下，我们在实现滑动门的时候，用以下HTML结构就可以满足需要：</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="tabs"&gt;
    &lt;</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;</span>标签一<span style="color: blue">&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;</span>标签二<span style="color: blue">&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span>区域一<span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span>区域二<span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a>借助 <a title="jQuery" rel="nofollow" href="http://jquery.com/" target="_blank">jQuery</a> 库，我们可以通过 $(“.tabs”)找到要实现的标签，然后 .find(“li”) 来找到要添加事件的元素，绑定事件的时候，我们可以通过该元素在$(“.tabs li”)集合中的索引值来明确是哪一个标签被激活，然后对应索引值的panel显示。代码类似这样：</p>
<pre class="code" style="width: 96.14%; height: 360px;"><span style="color: blue">&lt;</span><span style="color: #a31515">script </span><span style="color: red">type</span><span style="color: blue">="text/javascript"&gt;
    </span>$(<span style="color: blue">function</span>() {
        $(<span style="color: #a31515">".tabs"</span>).find(<span style="color: #a31515">"li"</span>).onmouseover(<span style="color: blue">function</span>(e) {
            <span style="color: blue">if </span>(e.target == <span style="color: blue">this</span>) {
                <span style="color: blue">var </span>tabs = $(<span style="color: blue">this</span>).parent().children(<span style="color: #a31515">"li"</span>);
                <span style="color: blue">var </span>panels = $(<span style="color: blue">this</span>).parent().parent().children(<span style="color: #a31515">"div"</span>);
                <span style="color: blue">var </span>index = $.inArray(<span style="color: blue">this</span>, tabs);
                <span style="color: blue">if </span>(panels.eq(index)[0]) {
                    tabs.removeClass(<span style="color: #a31515">"ui-tabs-selected"</span>)
                        .eq(index).addClass(<span style="color: #a31515">"ui-tabs-selected"</span>);
                    panels.addClass(<span style="color: #a31515">"ui-tabs-hide"</span>)
                        .eq(index).removeClass(<span style="color: #a31515">"ui-tabs-hide"</span>);
                }
            }
        });
    });
<span style="color: blue">&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>这段代码只使用了两个css类来处理，并且，自动判断tabs和panels的对应状态，假如你有4个tab，但是只有前三个启用了，那么你只需要写三个panel就可以，第四个panel不存在，则第四个tab自动不生效。</p>
<p>在实际使用中，会遇到一个问题，一般我们会给tab中的文字加链接，那么当鼠标滑过这个tab的时候，如果指到了文字，那么激发事件的对象有可能是a元素而不是li元素，则事件就不能正确激发。所以我们改进代码如下：</p>
<pre class="code" style="width: 99.41%; height: 362px;"><span style="color: blue">&lt;</span><span style="color: #a31515">script </span><span style="color: red">type</span><span style="color: blue">="text/javascript"&gt;
    </span>$(<span style="color: blue">function</span>() {
        $(<span style="color: #a31515">".ui-tabs-nav &gt; li &gt; a"</span>)onmouseover(<span style="color: blue">function</span>(e) {
            <span style="color: blue">if </span>(e.target == <span style="color: blue">this</span>) {
                <span style="color: blue">var </span>tabs = $(<span style="color: blue">this</span>).parent().parent().children(<span style="color: #a31515">"li"</span>);
                <span style="color: blue">var </span>panels = $(<span style="color: blue">this</span>).parent().parent().parent().children(<span style="color: #a31515">".ui-tabs-panel"</span>);
                <span style="color: blue">var </span>index = $.inArray(<span style="color: blue">this</span>, tabs);
                <span style="color: blue">if </span>(panels.eq(index)[0]) {
                    tabs.removeClass(<span style="color: #a31515">"ui-tabs-selected"</span>)
                        .eq(index).addClass(<span style="color: #a31515">"ui-tabs-selected"</span>);
                    panels.addClass(<span style="color: #a31515">"ui-tabs-hide"</span>)
                        .eq(index).removeClass(<span style="color: #a31515">"ui-tabs-hide"</span>);
                }
            }
        });
    });
<span style="color: blue">&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>与此对应的HTML结构是：</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">ul </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-nav"&gt;
        &lt;</span><span style="color: #a31515">li </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-selected"&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="/bbs"&gt;</span>论坛新帖<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">li</span><span style="color: blue">&gt;&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="/blog"&gt;</span>最新博文<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;&lt;/</span><span style="color: #a31515">li</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">ul</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-panel"&gt;
      </span><span style="color: green">&lt;!--这里调用最新论坛文章--&gt;
    </span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">div </span><span style="color: red">class</span><span style="color: blue">="ui-tabs-panel ui-tabs-hide"&gt;
      </span><span style="color: green">&lt;!--这里调用最新博客文章--&gt;
    </span><span style="color: blue">&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">div</span><span style="color: blue">&gt;</span></pre>
<p>同时，我们有以下的css类定义：</p>
<pre class="code"><span style="color: #a31515">.ui-tabs-nav
</span>{
    <span style="color: green">/*导航容器定义*/
</span>}
<span style="color: #a31515">.ui-tabs-nav li
</span>{
    <span style="color: green">/*默认标签样式*/
</span>}
<span style="color: #a31515">.ui-tabs-nav li.ui-tabs-selected
</span>{
    <span style="color: green">/*激活的标签样式*/
</span>}
<span style="color: #a31515">.ui-tabs-panel
</span>{
    <span style="color: green">/*默认的显示区域样式*/
</span>}
<span style="color: #a31515">.ui-tabs-hide
</span>{
    <span style="color: red">display</span>: <span style="color: blue">none</span>;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>这样，就可以根据你的需要，结合自己的css，定制不同样式的滑动门了。把相应的js代码放到页面中，那么在页面里任何地方只要你按照HTML结构编写了一段html，这段html就会自动变成滑动门。而不用在每个页面里单独指定特定的selector来应用滑动门的tabs()方法。并且，根据需要给你的滑动门标签添加需要的链接，或者不要链接（href=”#” 或者 href=”javascript:void(0)”）。</p>
<p>这段滑动门代码只要具有jQuery core 就可以正常运行，不需要加载jQuery UI。非常简单，而且很通用，样式上喜欢怎么扩展都可以。</p>
<p>具体效果可以参见 <a title="台海网首页" rel="external" href="http:/www.taihainet.com" target="_blank">http:/www.taihainet.com</a> ， 在 <a title="台海网" rel="external" href="http://www.taihainet.com/" target="_blank">台海网</a> 首页中，我一共应用了四个样式共九组滑动门，代码就只是上面给出的那一段。四个样式列举如下：</p>
<p><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb12.png" border="0" alt="image" /> <br />
<em>滑动门一：多个搜索表单，暂时只实现两个，后面三个由于没有对应的ui-tabs-panel，自动禁用，但是链接可以点击。</em></p>
<p><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb13.png" border="0" alt="image" /> <br />
<em>滑动门二：多块商务信息区域，其中第三个由于没有对应的ui-tabs-panel，自动禁用。</em></p>
<p><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb14.png" border="0" alt="image" /></p>
<p><em>滑动门三：新闻栏目切换，标签中的文字链接到对应的新闻栏目。</em></p>
<p><img style="display: inline; border: 0px;" title="image" src="/attachments/2009/04/image-thumb15.png" border="0" alt="image" /></p>
<p><em>滑动门四：论坛分板块帖子调用。标签中的文字链接到对应的论坛板块。</em></p>
<h3  class="related_post_title">相关日志</h3><ul class="related_post"><li>2009-12-21 -- <a href="http://ofcss.com/2009/12/21/web-developer-understand-css-box-model.html" title="前台开发从头说起：理解css盒模型">前台开发从头说起：理解css盒模型</a></li><li>2009-12-16 -- <a href="http://ofcss.com/2009/12/16/web-developer-about-css-selector.html" title="前台开发从头说起：谈谈CSS选择符">前台开发从头说起：谈谈CSS选择符</a></li><li>2009-12-15 -- <a href="http://ofcss.com/2009/12/15/web-developer-from-web-standard.html" title="前台开发从头说起：关于web标准化">前台开发从头说起：关于web标准化</a></li><li>2009-06-22 -- <a href="http://ofcss.com/2009/06/22/whatever-hover-pseudo-class-without-javascript.html" title="Whatever:hover &#8211; 无需javascript让IE支持丰富伪类">Whatever:hover &#8211; 无需javascript让IE支持丰富伪类</a></li><li>2006-04-04 -- <a href="http://ofcss.com/2006/04/04/dynamic-mouse-event-of-image-transparency.html" title="动态实现指定图片半透明及鼠标事件">动态实现指定图片半透明及鼠标事件</a></li><li>2006-03-28 -- <a href="http://ofcss.com/2006/03/28/simulate-window-in-center-with-css.html" title="CSS:模拟Windows窗口及DIV居中">CSS:模拟Windows窗口及DIV居中</a></li><li>2011-03-14 -- <a href="http://ofcss.com/2011/03/14/browser-rendering-optimizer-translation-extra.html" title="对《优化浏览器渲染》的补充【译】">对《优化浏览器渲染》的补充【译】</a></li><li>2011-03-12 -- <a href="http://ofcss.com/2011/03/12/flexible-two-column-layout-reloaded.html" title="自适应的多列图文混排改进">自适应的多列图文混排改进</a></li><li>2010-10-30 -- <a href="http://ofcss.com/2010/10/30/bfc-element-margin-bug-in-webkit.html" title="webkit中BFC元素临近浮动元素时的边距bug">webkit中BFC元素临近浮动元素时的边距bug</a></li><li>2010-10-20 -- <a href="http://ofcss.com/2010/10/20/clearfix-reloaded-overflowhidden-demystified-translation.html" title="clearfix改良及overflow:hidden详解【译】">clearfix改良及overflow:hidden详解【译】</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://ofcss.com/2009/04/13/portable-interchangeable-jquery-based-tabs.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

