使用 IndexedDB

<!DOCTYPE html>
<!--[if IE 6]>
<html id="ie6" lang="zh-CN">
<![endif]-->
<!--[if IE 7]>
<html id="ie7" lang="zh-CN">
<![endif]-->
<!--[if IE 8]>
<html id="ie8" lang="zh-CN">
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
<html lang="zh-CN">
<!--<![endif]-->
<head>
	<meta charset="UTF-8" />
	<title>IndexedDB 开发指南 | Node.js 教程|培训|咨询</title>
			
	
	<link rel="pingback" href="http://www.tfan.org/xmlrpc.php" />

		<!--[if lt IE 9]>
	<script src="http://www.tfan.org/wp-content/themes/Divi/js/html5.js" type="text/javascript"></script>
	<![endif]-->

	<script type="text/javascript">
		document.documentElement.className = 'js';
	</script>

	
<!-- All in One SEO Pack 2.2.7.1 by Michael Torbert of Semper Fi Web Design[318,356] -->
<link rel="author" href="http://princetoad@gmail.com" />
<meta name="description" itemprop="description" content="IndexedDB 是一种可以让你在用户的浏览器内持久化存储数据的方法。本文翻译自 MDN -> IndexedDB -> Using IndexedDB 的一篇长文。针对最新规范的 IndexedDB API 进行了详细讲解,针对 IndexedDB 的设计原理,基本模式,使用方法,常见错误及处理都进行了详细的讲解。" />

<meta name="keywords" itemprop="keywords" content="indexeddb,object store,游标,事务,,html5" />

<link rel="canonical" href="http://www.tfan.org/using-indexeddb/" />
<!-- /all in one seo pack -->
<link rel="alternate" type="application/rss+xml" title="Node.js 教程|培训|咨询 » Feed" href="http://www.tfan.org/feed/" />
<link rel="alternate" type="application/rss+xml" title="Node.js 教程|培训|咨询 » 评论Feed" href="http://www.tfan.org/comments/feed/" />
<link rel="alternate" type="application/rss+xml" title="Node.js 教程|培训|咨询 » 使用 IndexedDB评论Feed" href="http://www.tfan.org/using-indexeddb/feed/" />
		<script type="text/javascript">
			window._wpemojiSettings = {"baseUrl":"http:\/\/s.w.org\/images\/core\/emoji\/72x72\/","ext":".png","source":{"concatemoji":"http:\/\/www.tfan.org\/wp-includes\/js\/wp-emoji-release.min.js?ver=4.3"}};
			!function(a,b,c){function d(a){var c=b.createElement("canvas"),d=c.getContext&&c.getContext("2d");return d&&d.fillText?(d.textBaseline="top",d.font="600 32px Arial","flag"===a?(d.fillText(String.fromCharCode(55356,56812,55356,56807),0,0),c.toDataURL().length>3e3):(d.fillText(String.fromCharCode(55357,56835),0,0),0!==d.getImageData(16,16,1,1).data[0])):!1}function e(a){var c=b.createElement("script");c.src=a,c.type="text/javascript",b.getElementsByTagName("head")[0].appendChild(c)}var f,g;c.supports={simple:d("simple"),flag:d("flag")},c.DOMReady=!1,c.readyCallback=function(){c.DOMReady=!0},c.supports.simple&&c.supports.flag||(g=function(){c.readyCallback()},b.addEventListener?(b.addEventListener("DOMContentLoaded",g,!1),a.addEventListener("load",g,!1)):(a.attachEvent("onload",g),b.attachEvent("onreadystatechange",function(){"complete"===b.readyState&&c.readyCallback()})),f=c.source||{},f.concatemoji?e(f.concatemoji):f.wpemoji&&f.twemoji&&(e(f.twemoji),e(f.wpemoji)))}(window,document,window._wpemojiSettings);
		</script>
		<meta content="Divi v.2.4.6.1" name="generator"/><style type="text/css">
img.wp-smiley,
img.emoji {
	display: inline !important;
	border: none !important;
	box-shadow: none !important;
	height: 1em !important;
	width: 1em !important;
	margin: 0 .07em !important;
	vertical-align: -0.1em !important;
	background: none !important;
	padding: 0 !important;
}
</style>
<link rel='stylesheet' id='toc-screen-css'  href='http://www.tfan.org/wp-content/plugins/table-of-contents-plus/screen.css?ver=1505' type='text/css' media='all' />
<link rel='stylesheet' id='divi-fonts-css'  href='http://fonts.useso.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800&subset=latin,latin-ext' type='text/css' media='all' />
<link rel='stylesheet' id='et-gf-chinese-css'  href='//fonts.useso.com/earlyaccess/cwtexfangsong.css' type='text/css' media='all' />
<link rel='stylesheet' id='divi-style-css'  href='http://www.tfan.org/wp-content/themes/Divi/style.css?ver=2.4.6.1' type='text/css' media='all' />
<link rel='stylesheet' id='et-shortcodes-css-css'  href='http://www.tfan.org/wp-content/themes/Divi/epanel/shortcodes/css/shortcodes.css?ver=3.0' type='text/css' media='all' />
<link rel='stylesheet' id='et-shortcodes-responsive-css-css'  href='http://www.tfan.org/wp-content/themes/Divi/epanel/shortcodes/css/shortcodes_responsive.css?ver=3.0' type='text/css' media='all' />
<script type='text/javascript' src='http://www.tfan.org/wp-includes/js/jquery/jquery.js?ver=1.11.3'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.2.1'></script>
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.tfan.org/xmlrpc.php?rsd" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://www.tfan.org/wp-includes/wlwmanifest.xml" /> 
<link rel='prev' title='没有 setVersion 了,现在是 onupgradeneeded !' href='http://www.tfan.org/no-setversion-now-onupgradeneeded/' />
<link rel='next' title='如何在 MAC 下面使用 Aircrack-ng?' href='http://www.tfan.org/using-aircrack-ng-under-mac/' />
<meta name="generator" content="WordPress 4.3" />
<link rel='shortlink' href='http://www.tfan.org/?p=1371' />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />		<style id="theme-customizer-css">
																																				
		
																										
		
		
		@media only screen and ( min-width: 981px ) {
													h1 { font-size: 24px; }
				h2, .product .related h2, .et_pb_column_1_2 .et_quote_content blockquote p { font-size: 20px; }
				h3 { font-size: 17px; }
				h4, .et_pb_circle_counter h3, .et_pb_number_counter h3, .et_pb_column_1_3 .et_pb_post h2, .et_pb_column_1_4 .et_pb_post h2, .et_pb_blog_grid h2, .et_pb_column_1_3 .et_quote_content blockquote p, .et_pb_column_3_8 .et_quote_content blockquote p, .et_pb_column_1_4 .et_quote_content blockquote p, .et_pb_blog_grid .et_quote_content blockquote p, .et_pb_column_1_3 .et_link_content h2, .et_pb_column_3_8 .et_link_content h2, .et_pb_column_1_4 .et_link_content h2, .et_pb_blog_grid .et_link_content h2, .et_pb_column_1_3 .et_audio_content h2, .et_pb_column_3_8 .et_audio_content h2, .et_pb_column_1_4 .et_audio_content h2, .et_pb_blog_grid .et_audio_content h2, .et_pb_column_3_8 .et_pb_audio_module_content h2, .et_pb_column_1_3 .et_pb_audio_module_content h2, .et_pb_gallery_grid .et_pb_gallery_item h3, .et_pb_portfolio_grid .et_pb_portfolio_item h2, .et_pb_filterable_portfolio_grid .et_pb_portfolio_item h2 { font-size: 14px; }
				.et_pb_slide_description h2 { font-size: 36px; }
				.woocommerce ul.products li.product h3, .woocommerce-page ul.products li.product h3, .et_pb_gallery_grid .et_pb_gallery_item h3, .et_pb_portfolio_grid .et_pb_portfolio_item h2, .et_pb_filterable_portfolio_grid .et_pb_portfolio_item h2, .et_pb_column_1_4 .et_pb_audio_module_content h2 { font-size: 12px; }
										.footer-widget h4 { font-size: 18px; }
																											
					}
		@media only screen and ( min-width: 1350px) {
			.et_pb_row { padding: 27px 0; }
			.et_pb_section { padding: 54px 0; }
			.single.et_pb_pagebuilder_layout.et_full_width_page .et_post_meta_wrapper { padding-top: 81px; }
			.et_pb_section.et_pb_section_first { padding-top: inherit; }
			.et_pb_fullwidth_section { padding: 0; }
		}
		@media only screen and ( max-width: 980px ) {
																				}
		@media only screen and ( max-width: 767px ) {
														}
	</style>

	<style class="et_one_font_languages">h1, h2, h3, h4, h5, h6, body, input, textarea, select { font-family: 'cwTeXFangSong', serif; }</style>
	
	<style id="module-customizer-css">
			</style>

	</head>
<body class="single single-post postid-1371 single-format-standard et_pb_button_helper_class et_fixed_nav et_show_nav et_cover_background et_pb_gutter windows et_pb_gutters3 et_primary_nav_dropdown_animation_fade et_secondary_nav_dropdown_animation_fade et_pb_footer_columns4 et_header_style_left et_right_sidebar chrome">
	<div id="page-container">

	
		<header id="main-header" data-height-οnlοad="66">
			<div class="container clearfix et_menu_container">
							<div class="logo_container">
					<span class="logo_helper"></span>
					<a href="http://www.tfan.org/">
						<img src="http://www.tfan.org/wp-content/uploads/node250.png" alt="Node.js 教程|培训|咨询" id="logo" />
					</a>
				</div>
				<div id="et-top-navigation">
					<nav id="top-menu-nav">
					<ul id="top-menu" class="nav"><li id="menu-item-1404" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-home menu-item-1404"><a href="http://www.tfan.org">首页</a></li>
<li id="menu-item-1995" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1995"><a href="http://www.tfan.org/category/node/">Node.js</a></li>
<li id="menu-item-1993" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1993"><a href="http://www.tfan.org/category/js/">JavaScript</a></li>
<li id="menu-item-1994" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-1994"><a href="http://www.tfan.org/category/mongodb/">MongoDB</a></li>
<li id="menu-item-2676" class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2676"><a href="http://www.tfan.org/category/design/">设计</a></li>
</ul>					</nav>

					
										<div id="et_top_search">
						<span id="et_search_icon"></span>
					</div>
					
					<div id="et_mobile_nav_menu">
			<div class="mobile_nav closed">
				<span class="select_page">选择页面</span>
				<span class="mobile_menu_bar"></span>
			</div>
		</div>				</div> <!-- #et-top-navigation -->
			</div> <!-- .container -->
			<div class="et_search_outer">
				<div class="container et_search_form_container">
					<form role="search" method="get" class="et-search-form" action="http://www.tfan.org/">
					<input type="search" class="et-search-field" placeholder="搜索 …" value="" name="s" title="搜索:" />					</form>
					<span class="et_close_search_field"></span>
				</div>
			</div>
		</header> <!-- #main-header -->

		<div id="et-main-area">
<div id="main-content">
	<div class="container">
		<div id="content-area" class="clearfix">
			<div id="left-area">
							
				<article id="post-1371" class="et_pb_post post-1371 post type-post status-publish format-standard hentry category-html5-2 tag-html5 tag-indexeddb">
											<div class="et_post_meta_wrapper">
							<h1>使用 IndexedDB</h1>

						<p class="post-meta"> 由 <a href="http://www.tfan.org/author/tfan/" title="发表人 范圣刚" rel="author">范圣刚</a> | 十二 18, 2012 | <a href="http://www.tfan.org/category/html5-2/" rel="category tag">HTML5</a> | <span class="comments-number"><a href="http://www.tfan.org/using-indexeddb/#respond">0 条评论</a></span></p>
												</div> <!-- .et_post_meta_wrapper -->
				
					<div class="entry-content">
					<p>IndexedDB 是一种可以让你在用户的浏览器内持久化存储数据的方法。IndexedDB 为生成 Web Application 提供了丰富的查询能力,使我们的应用在在线和离线时都可以正常工作。</p>
<div id="toc_container" class="no_bullets"><p class="toc_title">目录</p><ul class="toc_list"><li><a href="#i"><span class="toc_number toc_depth_1">1</span> 关于本文档</a></li><li><a href="#i-2"><span class="toc_number toc_depth_1">2</span> 基本模式</a></li><li><a href="#i-3"><span class="toc_number toc_depth_1">3</span> 生成和构建一个对象存储空间</a><ul><li><a href="#_IndexedDB"><span class="toc_number toc_depth_2">3.1</span> 使用实验版本的 IndexedDB</a></li><li><a href="#i-4"><span class="toc_number toc_depth_2">3.2</span> 打开数据库</a><ul><li><a href="#i-5"><span class="toc_number toc_depth_3">3.2.1</span> 生成处理函数</a></li><li><a href="#i-6"><span class="toc_number toc_depth_3">3.2.2</span> 错误处理</a></li></ul></li><li><a href="#i-7"><span class="toc_number toc_depth_2">3.3</span> 创建和更新数据库版本号</a></li><li><a href="#i-8"><span class="toc_number toc_depth_2">3.4</span> 构建数据库</a></li></ul></li><li><a href="#i-9"><span class="toc_number toc_depth_1">4</span> 增加和删除数据</a><ul><li><a href="#i-10"><span class="toc_number toc_depth_2">4.1</span> 向数据库中增加数据</a></li></ul></li><li><a href="#i-11"><span class="toc_number toc_depth_1">5</span> 从数据库中删除数据</a></li><li><a href="#i-12"><span class="toc_number toc_depth_1">6</span> 从数据库中获取数据</a></li><li><a href="#i-13"><span class="toc_number toc_depth_1">7</span> 使用游标</a><ul><li><a href="#i-14"><span class="toc_number toc_depth_2">7.1</span> 使用索引</a></li><li><a href="#i-15"><span class="toc_number toc_depth_2">7.2</span> 指定游标的范围和方向</a></li></ul></li><li><a href="#_web_app"><span class="toc_number toc_depth_1">8</span> 当一个 web app 在另一个标签页中被打开时的版本变更</a></li><li><a href="#i-16"><span class="toc_number toc_depth_1">9</span> 安全</a></li><li><a href="#_IndexedDB-2"><span class="toc_number toc_depth_1">10</span> 一个完整的 IndexedDB 示例</a><ul><li><a href="#HTML"><span class="toc_number toc_depth_2">10.1</span> HTML 内容</a></li><li><a href="#CSS"><span class="toc_number toc_depth_2">10.2</span> CSS 内容</a></li><li><a href="#JavaScript"><span class="toc_number toc_depth_2">10.3</span> JavaScript 内容</a></li></ul></li><li><a href="#i-17"><span class="toc_number toc_depth_1">11</span> 下一步</a></li><li><a href="#i-18"><span class="toc_number toc_depth_1">12</span> 另请参阅</a></li></ul></div>
<h2 id=".E5.85.B3.E4.BA.8E.E6.9C.AC.E6.96.87.E6.A1.A3"><span id="i">关于本文档</span></h2>
<p>本篇教程将教会你如何使用 IndexedDB 的异步 API。如果你对 IndexedDB 还不熟悉,你应该首先阅读<a title="https://developer.mozilla.org/en/IndexedDB/Basic_Concepts_Behind_IndexedDB" href="https://developer.mozilla.org/en/IndexedDB/Basic_Concepts_Behind_IndexedDB">有关 IndexedDB 的基本概念</a>。</p>
<p>有关 IndexedDB API 的参考手册,请参见 <a title="https://developer.mozilla.org/en/IndexedDB" href="https://developer.mozilla.org/en/IndexedDB">IndexedDB</a> 这篇文章及其子页面,包括 IndexedDB 使用的对象类型,以及同步和异步 API 的方法。</p>
<h2 id="pattern"><span id="i-2">基本模式</span></h2>
<p>IndexedDB 鼓励使用的基本模式如下所示:</p>
<ol>
<li>打开数据库并且开始一个事务。</li>
<li>创建一个 object store。</li>
<li>构建一个请求来执行一些数据库操作,像增加或提取数据等。</li>
<li>通过监听正确类型的 DOM 事件以等待操作完成。</li>
<li>在操作结果上进行一些操作(可以在 request 对象中找到)<span id="more-1371"></span></li>
</ol>
<h2 id=".E7.94.9F.E6.88.90.E5.92.8C.E6.9E.84.E9.80.A0.E4.B8.80.E4.B8.AA_store"><span id="i-3">生成和构建一个对象存储空间</span></h2>
<p>由于 IndexedDB 本身的规范还在持续演进中,当前的 IndexedDB 的实现还是使用浏览器前缀。在规范更加稳定之前,浏览器厂商对于标准 IndexedDB API 可能都会有不同的实现。但是一旦大家对规范达成共识的话,厂商就会不带前缀标记地进行实现。实际上一些实现已经移除了浏览器前缀:IE 10,Firefox 16 和 Chrome 24。当使用前缀的时候,基于 Gecko 内核的浏览器使用 <code>moz</code> 前缀,基于 WebKit 内核的浏览器会使用 <code>webkit</code> 前缀。</p>
<h3 id="Using_an_experimental_version_of_IndexedDB"><span id="_IndexedDB">使用实验版本的 IndexedDB</span></h3>
<p>如果你希望在仍旧使用前缀的浏览器中测试你的代码, 可以使用下列代码:</p>
<pre class="brush: js">// In the following line, you should include the prefixes of implementations you want to test.
window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
// DON'T use "var indexedDB = ..." if you're not in a function.
// Moreover, you may need references to some window.IDB* objects:
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange
// (Mozilla has never prefixed these objects, so we don't need window.mozIDB*)
[/pre]

要注意的是使用前缀的实现可能会有问题,或者是实现的并不完整,也可能遵循的还是旧版的规范。因此不建议在生产环境中使用。我们更倾向于明确的不支持某一浏览器,而不是声称支持但是实际运行中却出问题:</pre>
<pre class="brush: js">if (!window.indexedDB) {
    window.alert("Your browser doesn't support a stable version of IndexedDB. Such and such feature will not be available.")
}</pre>
<h3 id="Opening_a_database"><span id="i-4">打开数据库</span></h3>
<p>我们像下面这样开始整个过程:</p>
<pre class="brush: js">// 打开我们的数据库
var request = window.indexedDB.open("MyTestDatabase");</pre>
<p>看到了吗? 打开数据库就像任何其他操作一样 — 你必须进行 “request”。</p>
<p><code>IndexedDB</code><code> </code>对象只有一个单一方法,<code>open()</code>, 当这个方法被调用时,打开名为 “MyTestDatabase”的数据库。 所有 IndexedDB 数据库都按照同样的 origin 进行存储, 所以 <code>mozilla.com</code> 可能有一个名为 “binky” 的数据库,同时 <code>mozilla.org</code> 也可以有一个完全不同的名为 “binky” 的数据库。如果该数据库不存在,则会被创建;如果已经存在,则被打开。</p>
<p>open 请求不会立即打开数据库或者开始一个事务。 对 <code>open()</code> 函数的调用会返回一个我们可以作为事件来处理的包含 result(成功的话)或者错误值的 <a title="/en-US/docs/IndexedDB/IDBOpenDBRequest" href="/en-US/docs/IndexedDB/IDBOpenDBRequest"><code>IDBOpenDBRequest</code></a> 对象。在 IndexedDB 中的大部分异步方法做的都是同样的事情 – 返回一个包含 result 或错误的 <a title="/en-US/docs/IndexedDB/IDBRequest" href="/en-US/docs/IndexedDB/IDBRequest"><code style="font-size: 14px; color: #333333;">IDBRequest</code></a> 对象。open 函数的结果是一个 <code style="font-size: 14px; color: #333333;"><a title="/en-US/docs/IndexedDB/IDBDatabase" href="/en-US/docs/IndexedDB/IDBDatabase">IDBDatabase</a>对象的实例。</code></p>
<p>该 open 方法接受第二个参数,就是数据库的版本号。这样我们就可以更新数据库的 schema ,也就是说如果我们打开的数据库不是我们期望的最新版本的话,我们可以对 object store 进行创建或是删除。在这种情况下,我们实现一个 <code>onupgradeneeded</code> 处理函数,在一个允许操作 object stores 的 <code>versionchange</code> 事务中 – 我们在后面的 <a href="#Updating_the_version_of_the_database">更新数据库的版本号</a>中会提到更多有关这方面的内容。使用标明期望的版本号来打开数据库的示例如下所示:</p>
<pre class="brush:js;">var request = indexedDB.open("MyTestDatabase", 3);</pre>
<h4 id="Generating_handlers"><span id="i-5">生成处理函数</span></h4>
<p>几乎所有我们产生的请求我们在处理的时候首先要做的就是添加成功和失败处理函数:</p>
<pre class="brush: js">request.onerror = function(event) {
  // Do something with request.errorCode!
};
request.onsuccess = function(event) {
  // Do something with request.result!
};</pre>
<p>这两个函数的哪一个,<code>onsuccess()</code> 还是 <code>onerror()</code>,会被调用呢?如果一切顺利的话,一个 success 事件(即一个 type 属性被设置成<code> "success"</code> 的 DOM 事件)<code></code>会被触发,使用 <code>request</code> 作为它的 <code>target</code>。 一旦它被触发的话,相关 <code>request</code> 的 <code>onsuccess()</code> 函数就会被触发,使用 success 事件作为它的参数。 否则,如果不是所有事情都成功的话,一个 error 事件(即<code> type</code> 属性被设置成 <code>"error"</code> 的 DOM 事件) 会在 request 上被触发。<code></code>这将会触发使用 error 事件作为参数的 <code></code><code>onerror()</code> 方法。</p>
<p>IndexedDB 的 API 被设计来尽可能地减少对错误处理的需求,所以你可能不会看到有很多的错误事件(起码,不会在你已经习惯了这些 API 之后!)。然而在打开数据库的情况下,还是有一些会产生错误事件的常见情况。最有可能出现的问题是用户决定不允许你的 web app 访问以创建一个数据库。IndexedDB 的主要设计目标之一就是允许大量数据可以被存储以供离线使用。(要了解关于针对每个浏览器你可以有多少存储空间的更多内容,请参见 <a title="https://developer.mozilla.org/en/IndexedDB#Storage_limits" href="/en/IndexedDB#Storage_limits">存储限制</a>)。</p>
<p>显然,浏览器不希望允许某些广告网络或恶意网站来污染你的计算机,所以浏览器会在任意给定的 web app 首次尝试打开一个 IndexedDB 存储时对用户进行提醒。用户可以选择允许访问或者拒绝访问。还有,IndexedDB 在浏览器的隐私模式(Firefox 的 Private Browsing 模式和 Chrome 的 Incognito 模式)下是被完全禁止的。 隐私浏览的全部要点在于不留下任何足迹,所以在这种模式下打开数据库的尝试就失败了。</p>
<p>现在,假设用户已经允许了你的要创建一个数据库的请求,同时你也已经收到了一个来触发 success 回调的 success 事件;然后呢?这里的 request 是通过调用 <code>indexedDB.open() </code>产生的, 所以 <code>request.result</code> 是一个 <code>IDBDatabase</code> 的实例,而且你肯定希望把它保存下来以供后面使用。你的代码看起来可能像这样:</p>
<pre class="brush: js">var db;
var request = indexedDB.open("MyTestDatabase");
request.onerror = function(event) {
  alert("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = function(event) {
  db = request.result;
};</pre>
<h4 id="Handling_Errors"><span id="i-6">错误处理</span></h4>
<p>如上文所述,错误事件冒泡出来。错误事件都是针对产生这些错误的请求的,然后事件冒泡到事务,然后最终到达数据库对象。如果你希望避免为所有的请求都增加错误处理程序,你可以替代性的仅对数据库对象添加一个错误处理程序,像这样:</p>
<pre class="brush: js">db.onerror = function(event) {
  // Generic error handler for all errors targeted at this database's
  // requests!
  alert("Database error: " + event.target.errorCode);
};</pre>
<p>在打开数据库时常见的可能出现的错误之一是 <code>VER_ERR</code>。这表明存储在磁盘上的数据库的版本高于你试图打开的版本。这是一种必须要被错误处理程序处理的一种出错情况。</p>
<h3 id="Creating_or_updating_the_version_of_the_database"><span id="i-7">创建和更新数据库版本号</span></h3>
<p><a name="Updating_the_version_of_the_database"></a>要更新数据库的 schema,也就是创建或者删除对象存储空间,需要实现<span style="line-height: 21px;"> </span><code style="font-size: 14px; color: #333333;">onupgradeneeded</code><span style="line-height: 21px;"> 处理程序,这个处理程序将会作为一个允许你处理对象存储空间的 </span><code style="font-size: 14px; color: #333333;">versionchange</code><span style="line-height: 21px;"> 事务的一部分被调用。</span></p>
<pre class="brush:js;">// 该事件仅在较新的浏览器中被实现
request.onupgradeneeded = function(event) { 
   // 更新对象存储空间和索引 .... 
};</pre>
<p>在数据库第一次被打开时或者当指定的版本号高于当前被持久化的数据库的版本号时,这个 <code>versionchange</code> 事务将被创建。</p>
<p>版本号是一个 <code>unsigned long long</code> 数字,这意味着它可以是一个非常大的整数。</p>
<div class="warning">
<p>这也意味着你不能使用浮点数,否则它会被转换成最接近的较小的整数并且事务可能不会启动,同样 <code>upgradeneeded</code> 事件也不会作为一个结果返回。例如不要使用 2.4 作为版本:</p>
<pre class="brush: js">var request = indexedDB.open("MyTestDatabase", 2.4); // don't do this, as the version will be rounded to 2</pre>
</div>
<p>WebKit 支持当前版本的规范但是只有 Chrome 23 及以上才支持。其他和更旧的实现没有实现当前版本的规范,因此还不支持 <code>indexedDB.open(name, version).onupgradeneeded</code> 签名。有关如何在较旧 Webkit 上升级数据库版本的更多信息,请参见 <a title="https://developer.mozilla.org/en/IndexedDB/IDBDatabase#setVersion()_.0A.0ADeprecated" href="/en/IndexedDB/IDBDatabase#setVersion()_.0A.0ADeprecated">IDBDatabase 参考文档</a>。</p>
<h3 id="Structuring_the_database"><span id="i-8">构建数据库</span></h3>
<p>现在来构建数据库。IndexedDB 使用对象存储空间而不是表,并且一个单独的数据库可以包含任意数量的对象存储空间。每当一个值被存储进一个对象存储空间时,它会被和一个键相关联。键的提供可以有几种不同的方法,这取决于对象存储空间是使用 <a title="https://developer.mozilla.org/en/IndexedDB#gloss_key_path" href="/en/IndexedDB#gloss_key_path">key path</a> 还是 <a title="en/IndexedDB#gloss key generator" href="/en/IndexedDB#gloss_key_generator">key generator</a>。</p>
<p>下面的表格显示了几种不同的提供键的方法。</p>
<table class="standard-table" border="1" cellspacing="1" cellpadding="1">
<thead>
<tr>
<th scope="col">Key Path</th>
<th scope="col">Key Generator</th>
<th scope="col">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>No</td>
<td>No</td>
<td>这种对象存储空间可以持有任意类型的值,甚至是像数字和字符串这种基本数据类型的值。每当我们想要增加一个新值的时候,必须提供一个单独的键参数。</td>
</tr>
<tr>
<td>Yes</td>
<td>No</td>
<td>这种对象存储空间只能持有 JavaScript 对象。这些对象必须具有一个和 key path 同名的属性。</td>
</tr>
<tr>
<td>No</td>
<td>Yes</td>
<td>这种对象存储空间可以持有任意类型的值。键会为我们自动生成,或者如果你想要使用一个特定键的话你可以提供一个单独的键参数。</td>
</tr>
<tr>
<td>Yes</td>
<td>Yes</td>
<td>这种对象存储空间只能持有 JavaScript 对象。通常一个键被生成的同时,生成的键的值被存储在对象中的一个和 key path 同名的属性中。然而,如果这样的一个属性已经存在的话,这个属性的值被用作键而不会生成一个新的键。</td>
</tr>
</tbody>
</table>
<p>你也可以使用对象存储空间持有的对象,不是基本数据类型,在任何对象存储空间上创建索引。索引可以让你使用被存储的对象的属性的值来查找存储在对象存储空间的值,而不是用对象的键来查找。</p>
<p>此外,索引具有对存储的数据执行简单限制的能力。通过在创建索引时设置 unique 标记,索引可以确保不会有两个具有同样索引 key path 值的对象被储存。因此,举例来说,如果你有一个用于持有一组 people 的对象存储空间,并且你想要确保不会有两个拥有同样 email 地址的 people,你可以使用一个带有 unique 标识的索引来确保这些。</p>
<p>这听起来可能有点混乱,但下面这个简单的例子应该可以演示这些个概念:</p>
<pre class="brush: js">// 我们的客户数据看起来像这样。
const customerData = [
  { ssn: "444-44-4444", name: "Bill", age: 35, email: "bill@company.com" },
  { ssn: "555-55-5555", name: "Donna", age: 32, email: "donna@home.org" }
];
const dbName = "the_name";

var request = indexedDB.open(dbName, 2);

request.onerror = function(event) {
  // 错误处理程序在这里。
};
request.onupgradeneeded = function(event) {
  var db = event.target.result;

  // 创建一个对象存储空间来持有有关我们客户的信息。
  // 我们将使用 "ssn" 作为我们的 key path 因为它保证是唯一的。
  var objectStore = db.createObjectStore("customers", { keyPath: "ssn" });

  // 创建一个索引来通过 name 搜索客户。
  // 可能会有重复的,因此我们不能使用 unique 索引。
  objectStore.createIndex("name", "name", { unique: false });

  // 创建一个索引来通过 email 搜索客户。
  // 我们希望确保不会有两个客户使用相同的 email 地址,因此我们使用一个 unique 索引。
  objectStore.createIndex("email", "email", { unique: true });

  // 在新创建的对象存储空间中保存值
  for (var i in customerData) {
    objectStore.add(customerData[i]);
  }
};</pre>
<p>正如前面提到的,<code>onupgradeneeded</code> 是我们唯一可以修改数据库结构的地方。在这里面,我们可以创建和删除对象存储空间以及构建和删除索引。</p>
<div>对象存储空间仅调用 <code>createObjectStore()</code> 就可以创建。这个方法使用存储空间的名称,和一个对象参数。即便这个参数对象是可选的,它还是非常重要的,因为它可以让你定义重要的可选属性和完善你希望创建的对象存储空间的类型。在我们的示例中,我们请求了一个名为“customers” 的对象存储空间并且定义了一个 使得存储空间中每个单独的对象都是唯一的属性作为 key path。在这个示例中的属性是 “ssn”,因为社会安全号码被确保是唯一的。被存储在对象存储空间中的所有对象都必须存在“ssn”。</div>
<p>我们也请求了一个名为 “name” 的着眼于存储的对象的 <code>name</code> 属性的索引。如同 <code>createObjectStore()</code>,<code>createIndex()</code> 使用了一个完善了我们希望创建的索引类型的可选的 <code>options</code> 对象。添加一个不带 <code>name</code> 属性的对象也会成功,但是这个对象不会出现在 “name” 索引中。</p>
<p>我们现在可以使用存储的用户对象的 <code>ssn</code> 直接从对象存储空间中把它们提取出来,或者通过使用索引来使用他们的 name 进行提取。要了解这些是如何实现的,请参见 <a title="Using IndexedDB#Using an index" href="/en/IndexedDB/Using_IndexedDB#Using_an_index">使用索引</a> 章节。</p>
<h2 id="Adding_and_removing_data"><span id="i-9">增加和删除数据</span></h2>
<p>在你可以对新数据库做任何事情之前,你需要开始一个事务。事务来自于数据库对象,而且你必须指定你想让这个事务跨越哪些对象存储空间。另外,你需要决定你是否将要对数据库进行更改或者你只是需要从它里面进行读取。虽然事务具有三种模式(只读,读写,和版本变更),在可以的情况下你最好还是使用只读事务,因为它们可以并发运行。</p>
<h3 id="Adding_data_to_the_database"><span id="i-10">向数据库中增加数据</span></h3>
<p>如果你刚刚创建了一个数据库,你可能想往里面写点东西。看起来会像下面这样:</p>
<pre class="brush:js;">var transaction = db.transaction(["customers"], "readwrite");
// 注意: 旧版实验性的实现使用不建议使用的常量 IDBTransaction.READ_WRITE 而不是 "readwrite"。
// 如果你想支持这样的实现,你只要这样写就可以了: 
// var transaction = db.transaction(["customers"], IDBTransaction.READ_WRITE);</pre>
<p><code>transaction()</code> 方法接受三个参数(虽然两个是可选的)并返回一个事务对象。第一个参数是事务希望跨越的对象存储空间的列表。如果你希望事务能够跨越所有的对象存储空间你可以传入一个空数组。如果你没有为第二个参数指定任何内容,你得到的是只读事务。因为这里我们是想要写入所以我们需要传入 <code>"readwrite"</code> 标识。</p>
<p>现在我们已经有了一个事务,我们需要理解它的生命周期。事务和事件循环的联系非常密切。如果你创建了一个事务但是并没有使用它就返回给事件循环,那么事务将变得无效。保持事务活跃的唯一方法就是在其上构建一个请求。当请求完成时你将会得到一个 DOM 事件,并且,假设请求成功了,你将会有另外一个机会在回调中来延长这个事务。如果你没有延长事务就返回到了事件循环,那么事务将会变得不活跃,依此类推。只要还有待处理的请求事务就会保持活跃。事务生命周期真的很简单但是可能需要一点时间你才能对它变得习惯。还有就是来几个例子也会有所帮助。如果你开始看到 <code>TRANSACTION_INACTIVE_ERR</code> 错误代码,那么你已经把某些事情搞乱了。</p>
<p>事务可以接收三种不同类型的 DOM 事件: <code>error</code>,<code>abort</code>,以及<code> complete</code>。我们已经讨论过 <code>error</code>事件冒泡,所以一个事务要接收所有可能产生错误事件的请求所产生的错误事件。更微妙的一点是一个 error 的默认行为是终止发生错误的事务。除非你在 error 事件上通过调用 <code>preventDefault()</code> 处理了这个错误,整个事务被回滚了。这样的设计迫使你去思考和处理错误,但是如果细粒度的错误处理太过繁琐的话,你也可以总是对数据库添加一个总的错误处理程序。如果你不处理一个错误事件或者你在事务中调用 <code>abort()</code>,那么事务被回滚并且有关事物的一个 <code>abort</code> 事件被触发。否则,在所有的未处理请求都完成后,你将得到一个 <code>complete</code> 事件。如果你正在做大量的数据库操作,那么追踪事务而不是单个的请求当然可以帮助你进行决断。</p>
<p>现在你有了一个事务了,你将需要从它拿到一个对象存储空间。事务只能让你拿到一个你在创建事务时已经指定过的对象存储空间。然后你可以增加所有你需要的数据。</p>
<p>现在你有了一个事务了,你将需要从它拿到一个对象存储空间。事务只能让你拿到一个你在创建事务时已经指定过的对象存储空间。然后你可以增加你需要的所有数据。</p>
<pre class="brush: js">// 当所有的数据都被增加到数据库时执行一些操作
transaction.oncomplete = function(event) {
  alert("All done!");
};

transaction.onerror = function(event) {
  // 不要忘记进行错误处理!
};

var objectStore = transaction.objectStore("customers");
for (var i in customerData) {
  var request = objectStore.add(customerData[i]);
  request.onsuccess = function(event) {
    // event.target.result == customerData[i].ssn
  };
}</pre>
<p>产生自<code> add()</code> 调用的请求的 <code>result </code>是被添加的值的键。因此在这种情况下,它应该等于被添加的对象的 <code>ssn</code> 属性, 因为对象存储空间使用 <code>ssn</code> 属性作为 key path。 注意 <code>add()</code> 函数要求数据库中不能已经有相同键的对象存在。如果你正在试图修改一个现有条目,或者你并不关心是否有一个同样的条目已经存在,使用 <code>put()</code>函数。</p>
<h2 id="Removing_data_from_the_database"><span id="i-11">从数据库中删除数据</span></h2>
<p>删除数据是非常类似的:</p>
<pre class="brush: js">var request = db.transaction(["customers"], "readwrite")
                .objectStore("customers")
                .delete("444-44-4444");
request.onsuccess = function(event) {
  // 删除数据成功!
};</pre>
<h2 id="Getting_data_from_the_database"><span id="i-12">从数据库中获取数据</span></h2>
<p>现在数据库里已经有了一些信息,你可以通过几种方法对它进行提取。首先是简单的 <code>get()</code>。你需要提供键来提取值,像这样:</p>
<pre class="brush: js">var transaction = db.transaction(["customers"]);
var objectStore = transaction.objectStore("customers");
var request = objectStore.get("444-44-4444");
request.onerror = function(event) {
  // 错误处理!
};
request.onsuccess = function(event) {
  // 对 request.result 做些操作!
  alert("Name for SSN 444-44-4444 is " + request.result.name);
};</pre>
<p>对于一个“简单”的提取这里的代码有点多了。下面看我们怎么把它再缩短一点,假设你在数据库的级别上来进行的错误处理:</p>
<pre class="brush: js">db.transaction("customers").objectStore("customers").get("444-44-4444").onsuccess = function(event) {
  alert("Name for SSN 444-44-4444 is " + event.target.result.name);
};</pre>
<p>这是如何工作的呢?由于只有一个对象存储空间,你可以避免传入一个在你的事务中需要的对象存储空间的列表,而只是作为一个字符串把名字传入即可。同样,你只是在从数据库读取数据,所以你不需要一个 <code>"readwrite"</code>事务。调用一个没有指定模式的 <code>transaction()</code>将给你一个 <code>"readonly"</code>事务。这里的另外一个微妙之处在于你实际上不需要保存请求对象到一个变量。因为 DOM 事件把这个请求作为它的 target,你可以使用 event 来得到 <code>result</code> 属性。很简单,对吧?!</p>
<h2 id="Using_a_cursor"><span id="i-13">使用游标</span></h2>
<p>使用 <code>get()</code> 要求你知道你想要检索哪一个键。如果你想要遍历对象存储空间中的所有值,那么你可以使用游标。看起来会像下面这样:</p>
<pre class="brush: js">var objectStore = db.transaction("customers").objectStore("customers");

objectStore.openCursor().onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    alert("Name for SSN " + cursor.key + " is " + cursor.value.name);
    cursor.continue();
  }
  else {
    alert("No more entries!");
  }
};</pre>
<p><code>openCursor()</code> 函数需要几个参数。首先,你可以使用一个 key range 对象来限制被检索的项目的范围。第二,你可以指定你希望进行迭代的方向。在上面的示例中,我们在以升序迭代所有的对象。游标成功的回调有点特别。游标对象本身是请求的 <code>result</code> (上面我们使用的是简写形式,所以是 <code>event.target.result</code>)。然后实际的 key 和 value 可以根据游标对象的 <code>key</code> 和 <code>value</code> 属性被找到。如果你想要保持继续前行,那么你必须调用游标上的 <code>continue()</code> 。当你已经到达数据的末尾时(或者没有匹配 <code>openCursor()</code> 请求的条目)你仍然会得到一个成功回调,但是 <code>result</code> 属性是 <code>undefined。</code></p>
<p>使用游标的一种常见模式是提取出在一个对象存储空间中的所有对象然后把它们添加到一个数组中,像这样:</p>
<pre class="brush: js">var customers = [];

objectStore.openCursor().onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    customers.push(cursor.value);
    cursor.continue();
  }
  else {
    alert("Got all customers: " + customers);
  }
};</pre>
<div class="warning"><strong>警告:</strong> 下面的函数并不是 IndexedDB 标准的一部分!</div>
<p>Mozilla 也已经实现了 <code>getAll()</code>来处理这种情况。它不是 IndexedDB 标准的一部分,所以它未来可能会消失。我们已经把它包含在这里是因为我们觉得它比较有用。下面的代码实现的是跟上面同样的事情:</p>
<pre class="brush: js">objectStore.getAll().onsuccess = function(event) {
  alert("Got all customers: " + event.target.result);
};</pre>
<p>查找游标的 <code>value</code>属性会引起相关的性能损耗,因为对象是被延迟创建的。当使用 <code>getAll()</code> 时,Gecko 必须立即创建所有的对象。如果你仅是对检索每个键感兴趣,举个例子,使用游标比使用 <code>getAll()</code>要高效的多。如果你试图获得一个对象存储空间内所有对象的一个数组,那么,使用<code> getAll()</code>。</p>
<h3 id="Using_an_index"><span id="i-14">使用索引</span></h3>
<p>使用 SSN 作为键来存储客户数据是合理的,因为 SSN 唯一地标识了一个个体(对隐私来说这是否是一个好的想法是另外一个话题,不在本文的讨论范围内)。如果你想要通过姓名来查找一个客户,那么,你将需要在数据库中迭代所有的 SSN 直到你找到正确的那个。以这种方式来查找将会非常的慢,相反你可以使用索引。</p>
<pre class="brush: js">var index = objectStore.index("name");
index.get("Donna").onsuccess = function(event) {
  alert("Donna's SSN is " + event.target.result.ssn);
};</pre>
<p>“name” 游标不是唯一的,因此 <code>name</code> 被设成 <code>"Donna"</code> 的记录可能不止一条。在这种情况下,你总是得到键值最小的那个。</p>
<p>如果你需要访问带有给定 <code>name</code> 的所有的记录你可以使用一个游标。你可以在索引上打开两个不同类型的游标。一个常规游标映射索引属性到对象存储空间中的对象。一个键索引映射索引属性到用来存储对象存储空间中的对象的键。不同之处被展示如下:</p>
<pre class="brush: js">index.openCursor().onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    // cursor.key 是一个 name, 就像 "Bill", 然后 cursor.value 是整个对象。
    alert("Name: " + cursor.key + ", SSN: " + cursor.value.ssn + ", email: " + cursor.value.email);
    cursor.continue();
  }
};

index.openKeyCursor().onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    // cursor.key is 一个 name, 就像 "Bill", 然后 cursor.value 是那个 SSN。
    // 没有办法可以得到存储对象的其余部分。
    alert("Name: " + cursor.key + ", "SSN: " + cursor.value);
    cursor.continue();
  }
};</pre>
<h3 id="Specifying_the_range_and_direction_of_cursors"><span id="i-15">指定游标的范围和方向</span></h3>
<p>如果你想要限定你在游标中看到的值的范围,你可以使用一个 key range 对象然后把它作为第一个参数传给 <code>openCursor()</code> 或是 <code>openKeyCursor()</code>。你可以构造一个只允许一个单一 key 的 key range,或者一个具有下限或上限,或者一个既有上限也有下限。边界可以是闭合的(也就是说 key range 包含给定的值)或者是“开放的”(也就是说 key range 不包括给定的值)。这里是它如何工作的:</p>
<pre class="brush: js">// 只匹配 "Donna"
var singleKeyRange = IDBKeyRange.only("Donna");

// 匹配所有在 "Bill" 前面的, 包括 "Bill"
var lowerBoundKeyRange = IDBKeyRange.lowerBound("Bill");

// 匹配所有在 “Bill” 前面的, 但是不需要包括 "Bill"
var lowerBoundOpenKeyRange = IDBKeyRange.lowerBound("Bill", true);

// Match anything up to, but not including, "Donna"
var upperBoundOpenKeyRange = IDBKeyRange.upperBound("Donna", true);

//Match anything between "Bill" and "Donna", but not including "Donna"
var boundKeyRange = IDBKeyRange.bound("Bill", "Donna", false, true);

index.openCursor(boundKeyRange).onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    // Do something with the matches.
    cursor.continue();
  }
};</pre>
<p>有时候你可能想要以倒序而不是正序(所有游标的默认顺序)来遍历。切换方向是通过传递 <code>prev</code> 到 <code>openCursor()</code> 方法来实现的:</p>
<pre class="brush: js">objectStore.openCursor(null, IDBCursor.prev).onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    // Do something with the entries.
    cursor.continue();
  }
};</pre>
<p>因为 “name” 索引不是唯一的,那就有可能存在具有相同 <code>name</code> 的多条记录。要注意的是这种情况不可能发生在对象存储空间上,因为键必须永远是唯一的。如果你想要在游标在索引迭代过程中过滤出重复的,你可以传递 <code>nextunique</code> (或 <code>prevunique</code> 如果你正在向后寻找)作为方向参数。 当 <code>nextunique</code> 或是 <code>prevunique</code> 被使用时,被返回的那个总是键最小的记录。</p>
<pre class="brush: js">index.openKeyCursor(null, IDBCursor.nextunique).onsuccess = function(event) {
  var cursor = event.target.result;
  if (cursor) {
    // Do something with the entries.
    cursor.continue();
  }
};</pre>
<h2 id="Version_changes_while_a_web_app_is_open_in_another_tab"><span id="_web_app">当一个 web app 在另一个标签页中被打开时的版本变更</span></h2>
<p>当你的 web app 在这样一种方式下改变你的数据库时碰到被要求进行版本变化,你需要考虑如果用户已经在一个标签页中打开了你的应用的旧版本的数据库,然后他又在另一个标签页中加载了你的应用的新版本,这种情况下会发生什么事情。当你带着比数据库实际版本更高的版本号调用 <code>open()</code> 时,所有其他打开的数据库必须在你开始实际对数据库进行修改之前显式通知这个请求。这里是它如何工作的:</p>
<pre class="brush: js">var openReq = mozIndexedDB.open("MyTestDatabase", 2);

openReq.onblocked = function(event) {
  // 如果其他标签页已经加载了这个数据库,那么
  // 在我们可以继续处理之前它需要被关闭。
  alert("Please close all other tabs with this site open!");
};

openReq.onupgradeneeded = function(event) {
  // 所有其它数据库都已经被关掉了。Set everything up.
  db.createObjectStore(/* ... */);
  useDatabase(db);
}  

openReq.onsuccess = function(event) {
  var db = event.target.result;
  useDatabase(db);
  return;
}

function useDatabase(db) {
  // 确保添加一个如果另一个页面请求一个版本变化时来被通知的处理程序。
  // 我们必须关闭这个数据库。这就允许其他页面对数据库进行升级。
  // 如果你不这么做的话,除非用户关闭标签页否则升级就不会发生。
  db.onversionchange = function(event) {
    db.close();
    alert("A new version of this page is ready. Please reload!");
  };

  // 其他针对数据库的处理
}</pre>
<h2 id="Security"><span id="i-16">安全</span></h2>
<p>IndexedDB 使用同源原则,这意味着它把存储空间绑定到了创建它的站点的源(典型情况下,就是站点的域或是子域),所以它不能被任何其他源访问。</p>
<p>要着重指出的一点是 IndexedDB 不适用于从另一个站点加载进框架的内容 (不管是 {{ HTMLElement(“frame”) }} 还是 {{ HTMLElement(“iframe”) }}。这是一项安全措施。为什么说这一点是很重要的,请查阅 {{ bug(595307) }}.</p>
<h2 id="Full_IndexedDB_example"><span id="_IndexedDB-2">一个完整的 IndexedDB 示例</span></h2>
<h3 id="HTML_Content"><span id="HTML">HTML 内容</span></h3>
<pre class="brush: html">    <h1>IndexedDB Demo: storing blobs, e-publications example</h1>
    <div id="msg">
    </div>

    <form id="register-form">
      <table>
        <tbody>
          <tr>
            <td>
              <label for="pub-title" class="required">
                Title:
              </label>
            </td>
            <td>
              <input type="text" id="pub-title" name="pub-title" />
            </td>
          </tr>
          <tr>
            <td>
              <label for="pub-year" class="required">
                Year:
              </label>
            </td>
            <td>
              <input type="number" id="pub-year" name="pub-year" />
            </td>
          </tr>
          <tr>
            <td>
              <label for="pub-biblioid" class="required">
                Bibliographic ID
                <span class="note">(ISBN, ISSN, etc.)</span>:
              </label>
            </td>
            <td>
              <input type="text" id="pub-biblioid" name="pub-biblioid"/>
            </td>
          </tr>
        </tbody>
        <tbody>
          <tr>
            <td>
              <label for="pub-file">
                File image:
              </label>
            </td>
            <td>
              <input type="file" id="pub-file" accept="image/*"/>
            </td>
          </tr>
          <tr>
            <td>
              <label for="pub-content-url">
                Online-file image URL:
              </label>
            </td>
            <td>
              <input type="text" id="pub-content-url" name="pub-content-url"/>
            </td>
          </tr>
        </tbody>
      </table>

      <div class="button-pane">
        <input type="button" id="add-button" value="Add Publication" />
        <input type="reset" id="register-form-reset"/>
        <span id="action-status"></span>
      </div>
    </form>

    <form id="delete-form">
      <div>
        <label for="pub-biblioid-to-delete" class="required">
          Key (for example 1, 2, 3, etc.):
        </label>
        <input type="text" id="pub-biblioid-to-delete"
               name="pub-biblioid-to-delete" />
      </div>
      <div class="button-pane">
        <input type="button" id="delete-button" value="Delete Publication" />
      </div>
    </form>

    <form id="search-form">
      <div class="button-pane">
        <input type="button" id="search-list-button"
               value="List database content" />
      </div>
    </form>

    <div>
      <div id="pub-msg">
      </div>
      <ul id="pub-list">
      </ul>
    </div></pre>
<h3 id="CSS_Content"><span id="CSS">CSS 内容</span></h3>
<pre class="brush: css">body {
  font-size: 0.8em;
  font-family: Sans-Serif;
}

form {
  background-color: #cccccc;
  border-radius: 0.3em;
  display: inline-block;
  margin-bottom: 0.5em;
  padding: 1em;
}

table {
  border-collapse: collapse;
}

input {
  padding: 0.3em;
  border-color: #cccccc;
  border-radius: 0.3em;
}

.required:after {
  content: "*";
  color: red;
}

.button-pane {
  margin-top: 1em;
}

#documents-list {
  background-color: #eeeeee;
}

.action-success {
  margin-left: 1em;
  padding: 0.5em;
  color: #00d21e;
  background-color: #eeeeee;
  border-radius: 0.2em;
}

.action-failure {
  margin-left: 1em;
  padding: 0.5em;
  color: #ff1408;
  background-color: #eeeeee;
  border-radius: 0.2em;
}

.presence-no {
  font-style: italic;
}

.note {
  font-size: smaller;
}</pre>
<h3 id="JavaScript_Content"><span id="JavaScript">JavaScript 内容</span></h3>
<pre class="brush: js">(function () {
  // Works with:
  // * Firefox >= 16.0
  // * Google Chrome >= 24.0 (you may need to get Google Chrome Canary)

  const DB_NAME = 'mdn-demo-indexeddb-epublications';
  const DB_VERSION = 1; // Use a long long for this value (don't use a float)
  const DB_STORE_NAME = 'publications';

  var db;

  function initDb() {
    console.debug("initDb ...");
    var req = indexedDB.open(DB_NAME, DB_VERSION);
    req.onsuccess = function (evt) {
      // Better use "this" than "req" to get the result to avoid problems with
      // garbage collection.
      // db = req.result;
      db = this.result;
      console.debug("initDb DONE");
    };
    req.onerror = function (evt) {
      console.error("initDb:", evt.target.errorCode);
    };

    req.onupgradeneeded = function (evt) {
      console.debug("initDb.onupgradeneeded");
      var store = evt.currentTarget.result.createObjectStore(
        DB_STORE_NAME, { keyPath: 'id', autoIncrement: true });

      store.createIndex('biblioid', 'biblioid', { unique: true });
      store.createIndex('title', 'title', { unique: false });
      store.createIndex('year', 'year', { unique: false });
    };
  }

  function getFile(key, success_callback) {
    var tx = db.transaction(DB_STORE_NAME, 'readonly');
    var store = tx.objectStore(DB_STORE_NAME);
    var req = store.get(key);
    req.onsuccess = function(evt) {
      var value = evt.target.result;
      if (value)
        success_callback(value.file);
    };
  }

  function displayPubList() {
    console.debug("displayPubList");
    var pub_msg = $('#pub-msg');
    pub_msg.empty();
    var pub_list = $('#pub-list');
    pub_list.empty();

    var tx = db.transaction(DB_STORE_NAME, 'readonly');
    var store = tx.objectStore(DB_STORE_NAME);
    var req;

    req = store.count();
    // Requests are executed in the order in which they were made against the
    // transaction, and their results are returned in the same order.
    // Thus the count text below will be displayed before the actual pub list
    // (not that it is algorithmically important in this case).
    req.onsuccess = function(evt) {
      pub_msg.append('<p>There are <strong>' + evt.target.result +
                     '</strong> record(s) in the object store.</p>');
    };
    req.onerror = function(evt) {
      console.error("add error", this.error);
      displayActionFailure(this.error);
    };

    var i = 0;
    var img_id;
    var file_presence;
    var presence_html;
    req = store.openCursor();
    req.onsuccess = function(evt) {
      var cursor = evt.target.result;
      if (cursor) {
        presence_html = "<span class='presence-no'>No image</span>";
        file_presence = cursor.value.file != null;
        console.debug("cursor.value:", cursor.value);
        if (file_presence) {
          img_id = 'pub-img-' + i;
          presence_html = '<img id="' + img_id + '"/>';
          getFile(cursor.key, function(file) {
            console.debug("file:", file);

            // Note that here it is not possible to set a link to the file to
            // make  it possible to download it.
            // The only possible options are:
            // * display the file if it is an image
            // * getting text/other info from the file and display them
            var obj_url = window.URL.createObjectURL(file);
            $('#' + img_id).attr('src', obj_url);
            window.URL.revokeObjectURL(obj_url);
          });
        }
        pub_list.append('<li>' +
                        '[' + cursor.key + '] ' +
                        '(biblioid: ' + cursor.value.biblioid + ') ' +
                        cursor.value.title + ' - ' +
                        cursor.value.year + ' / ' +
                        presence_html +
                        '</li>');

        // Move on to the next object in store
        cursor.continue();

        // This counter serves only to create distinct img ids
        i++;
      } else {
        console.debug("No more entries!");
      }
    };
  };

  function addPublication(biblioid, title, year, file) {
    console.debug("addPublication arguments:", arguments);
    if (!db) {
      console.error("addPublication: the db is not initialized");
      return;
    }
    var tx = db.transaction(DB_STORE_NAME, 'readwrite');
    var store = tx.objectStore(DB_STORE_NAME);
    var req = store.add({ biblioid: biblioid, title: title, year: year,
                          file: file });
    req.onsuccess = function (evt) {
      console.debug("Insertion in DB successful");
      displayPubList();
    };
    req.onerror = function() {
      console.error("add error", this.error);
      displayActionFailure(this.error);
    };
  }

  function displayActionSuccess(msg) {
    msg = typeof msg !== 'undefined' ? "Success: " + msg : "Success";
    $('#action-status').html('<span class="action-success">' + msg + '</span>');
  }
  function displayActionFailure(msg) {
    msg = typeof msg !== 'undefined' ? "Failure: " + msg : "Failure";
    $('#action-status').html('<span class="action-failure">' + msg + '</span>');
  }
  function resetActionStatus() {
    console.debug("resetActionStatus ...");
    $('#action-status').empty();
    console.debug("resetActionStatus DONE");
  }

  function addEventListeners() {
    console.debug("addEventListeners");
    initDb();

    $('#register-form-reset').click(function(evt) {
      resetActionStatus();
    });

    $('#add-button').click(function(evt) {
      console.debug("add ...");
      var title = $('#pub-title').val();
      var year = $('#pub-year').val();
      var biblioid = $('#pub-biblioid').val();
      if (!title || !year || !biblioid) {
        displayActionFailure("Required field(s) missing");
        return;
      }

      var file_input = $('#pub-file');
      var selected_file = file_input.get(0).files[0];
      console.debug("selected_file:", selected_file);
      file_input.val(null);
      var content_url = $('#pub-content-url').val();
      if (selected_file) {
        addPublication(biblioid, title, year, selected_file);
      } else {
        addPublication(biblioid, title, year);
        displayActionSuccess();
      }

    });

    $('#delete-button').click(function(evt) {
      console.debug("delete ...");
      var k = $('#pub-biblioid-to-delete').val();
      console.debug("delete k:", k);
      var tx = db.transaction(DB_STORE_NAME, 'readwrite');
      var store = tx.objectStore(DB_STORE_NAME);

      // Warning: The exact same key used for creation needs to be passed for
      // the deletion. If the key was a Number for creation, then it needs to be
      // a Number for deletion.
      k = Number(k);

      // The code that could be nice if it worked
      // var req = store.delete(k);
      // req.onsuccess = function(evt) {
      //   var record = evt.target.result;
      //   console.debug("record:", record);
      //   if (typeof record !== 'undefined') {
      //     displayActionSuccess("Deletion successful");
      //     displayPubList();
      //   } else {
      //     displayActionFailure("No matching record found");
      //   }
      // };
      // req.onerror = function (evt) {
      //   console.error("delete:", evt.target.errorCode);
      // };

      // The code that actually works
      //
      // As per spec http://www.w3.org/TR/IndexedDB/#object-store-deletion-operation
      // the result of the Object Store Deletion Operation algorithm is
      // undefined, so it's not possible to know if some records were actually
      // deleted by looking at the request result.
      var req = store.get(k);
      req.onsuccess = function(evt) {
        var record = evt.target.result;
        console.debug("record:", record);
        if (typeof record !== 'undefined') {
          req = store.delete(k);
          req.onsuccess = function(evt) {
            console.debug("evt:", evt);
            console.debug("evt.target:", evt.target);
            console.debug("evt.target.result:", evt.target.result);
            console.debug("delete successful");
            displayActionSuccess("Deletion successful");
            displayPubList();
          };
          req.onerror = function (evt) {
            console.error("delete:", evt.target.errorCode);
          };
        } else {
          displayActionFailure("No matching record found");
        }
      };
      req.onerror = function (evt) {
        console.error("delete:", evt.target.errorCode);
      };

    });

    var search_button = $('#search-list-button');
    search_button.click(function(evt) {
      displayPubList();
    });

  }

// function dbAddAndGetBlob(title, year, url) {
//   // Create XHR
//   var xhr = new XMLHttpRequest();
//   var blob;

//   // We can't use jQuery here because as of jQuery 1.7.2 the new "blob"
//   // responseType is not handled.
//   // http://bugs.jquery.com/ticket/7248
//   //
//   // Attention Same origin policy, the URL must come from the same origin than
//   // the web app.
//   xhr.open('GET', url, true);
//   // Set the responseType to blob
//   xhr.responseType = 'blob';
//   xhr.addEventListener("load", function () {
//                          if (xhr.status === 200) {
//                            console.debug("File retrieved");

//                            // Blob as response
//                            blob = xhr.response;
//                            console.debug("Blob:" + blob);

//                            // Insert the received blob into IndexedDB
//                            dbAdd(title, year, blob);
//                          } else {
//                            console.error("loadConfigFormHtmlContent error:",
//                                          xhr.responseText, xhr.status);
//                          }
//                        }, false);
//   // Send XHR
//   xhr.send();
// }

  addEventListeners();
})(); // Immediately-Invoked Function Expression (IIFE)</pre>
<h2 id="Next_step"><span id="i-17">下一步</span></h2>
<p>如果你想要多试试这些 API 的用法,请跳转到 <a title="https://developer.mozilla.org/en/IndexedDB" href="/en/IndexedDB">参考手册</a> 以查看其他不同的用法。</p>
<h2 id="See_also"><span id="i-18">另请参阅</span></h2>
<p>参考</p>
<ul>
<li><a title="https://developer.mozilla.org/en/IndexedDB" href="/en/IndexedDB">IndexedDB API 参考</a></li>
<li><a class="external" title="http://www.w3.org/TR/IndexedDB/" href="http://www.w3.org/TR/IndexedDB/">Indexed Database API 规范</a></li>
</ul>
<p>教程</p>
<ul>
<li><a class="external" title="http://www.html5rocks.com/tutorials/indexeddb/todo/" href="http://www.html5rocks.com/tutorials/indexeddb/todo/">A simple TODO list using HTML5 IndexedDB</a><span class="external">.  注意:该教程基于旧版本的规范因此无法在最新版的浏览器上正常工作-它仍然使用已经被移除的setVersion() 方法。<br />
</span></li>
<li><a title="http://www.html5rocks.com/en/tutorials/indexeddb/uidatabinding/" href="http://www.html5rocks.com/en/tutorials/indexeddb/uidatabinding/">Databinding UI Elements with IndexedDB</a></li>
</ul>
<p>相关文章</p>
<ul>
<li><a class="external" title="http://msdn.microsoft.com/en-us/scriptjunkie/gg679063.aspx" href="http://msdn.microsoft.com/en-us/scriptjunkie/gg679063.aspx">IndexedDB — The Store in Your Browser</a></li>
<li><a class="external" href="http://html5demos.tfan.org/storage">Storage – html5demos</a></li>
</ul>
<p>Firefox</p>
<ul>
<li>Mozilla <a class="link-https" title="https://mxr.mozilla.org/mozilla-central/find?text=&string=dom/indexedDB/.*\.idl&regexp=1" href="https://mxr.mozilla.org/mozilla-central/find?text=&string=dom%2FindexedDB%2F.*%5C.idl&regexp=1">interface files</a></li>
<li><a title="https://developer.mozilla.org/en/IndexedDB" href="/en/IndexedDB">Using JavaScript Generators in Firefox </a></li>
</ul>
<p>注:本文由范圣刚翻译自 MDN-> IndexedDB -> Using IndexedDB,因为水平有限,有翻译不当的地方请大家批评指正。英文原文链接:<a title="Using IndexedDB | MDN" href="https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB" target="_blank">https://developer.mozilla.org/en-US/docs/IndexedDB/Using_IndexedDB</a></p>
					</div> <!-- .entry-content -->
					<div class="et_post_meta_wrapper">
					
					<!-- You can start editing here. -->

<section id="comment-wrap">
		   <div id="comment-section" class="nocomments">
		  			 <!-- If comments are open, but there are no comments. -->

		  	   </div>
										<div id="respond" class="comment-respond">
				<h3 id="reply-title" class="comment-reply-title"><span>提交评论</span> <small><a rel="nofollow" id="cancel-comment-reply-link" href="/using-indexeddb/#respond" style="display:none;">取消回复</a></small></h3>
									<form action="http://www.tfan.org/wp-comments-post.php" method="post" id="commentform" class="comment-form">
																			<p class="comment-notes"><span id="email-notes">电子邮件地址不会被公开。</span> 必填项已用<span class="required">*</span>标注</p>							<p class="comment-form-author"><label for="author">姓名 <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" aria-required='true' required='required' /></p>
<p class="comment-form-email"><label for="email">电子邮件 <span class="required">*</span></label> <input id="email" name="email" type="text" value="" size="30" aria-describedby="email-notes" aria-required='true' required='required' /></p>
<p class="comment-form-url"><label for="url">站点</label> <input id="url" name="url" type="text" value="" size="30" /></p>
												<p class="comment-form-comment"><label for="comment">评论</label> <textarea id="comment" name="comment" cols="45" rows="8"  aria-required="true" required="required"></textarea></p>						
						<p class="form-submit"><input name="submit" type="submit" id="submit" class="submit et_pb_button" value="提交评论" /> <input type='hidden' name='comment_post_ID' value='1371' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</p><p style="display: none;"><input type="hidden" id="akismet_comment_nonce" name="akismet_comment_nonce" value="b8a6903e0a" /></p><p style="display: none;"><input type="hidden" id="ak_js" name="ak_js" value="226"/></p>					</form>
							</div><!-- #respond -->
				</section>					</div> <!-- .et_post_meta_wrapper -->
				</article> <!-- .et_pb_post -->

										</div> <!-- #left-area -->

				<div id="sidebar">
				<div id="recent-posts-4" class="et_pb_widget widget_recent_entries">		<h4 class="widgettitle">近期文章</h4>		<ul>
					<li>
				<a href="http://www.tfan.org/how-to-design-a-logo-with-illustrato/">教程:如何使用 Illustrator 来设计一个 Logo</a>
						</li>
					<li>
				<a href="http://www.tfan.org/linux-quick-tips/">Linux(CentOS) Quick Tips</a>
						</li>
					<li>
				<a href="http://www.tfan.org/frequently-used-mongodb-commands/">MongoDB 常用命令</a>
						</li>
					<li>
				<a href="http://www.tfan.org/nodejs-in-practice-at-clouda-mobile-51cto/">演讲:Node.js 软件开发实践分享</a>
						</li>
					<li>
				<a href="http://www.tfan.org/javascript-primitive-types-vs-reference-types/">JavaScript 中基本数据类型和引用类型的区别</a>
						</li>
					<li>
				<a href="http://www.tfan.org/sencha-touch-2-x-faq-application-views-synta/">Sencha Touch 2.x FAQ – 多级目录下 Ext.application 中 views 的写法</a>
						</li>
					<li>
				<a href="http://www.tfan.org/sencha-touch-2-x-faq-button-handler-and-controller-tap-event/">Sencha Touch 2.x FAQ – 同时定义了按钮的 handler 和 Controller 中的 tap 事件,那么当用户点击按钮时会执行哪一段处理代码?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/sencha-touch-2-x-faq-navigate-to-root-item/">Sencha Touch 2.x FAQ – navigationview 如何自任何节点下直接返回到根节点?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/sencha-touch-2-x-faq-namespacing-and-code-organization/">Sencha Touch 2.x FAQ – 把 app/view 下面的文件再用几个目录组织起来之后,程序应该相应的做什么样的修改?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/faq-configuration-required-to-add-view-to-tab-panel/">Sencha Touch 2.x FAQ – 增加一个 View 到 tabpanel 都需要哪些必备的配置信息 ?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/tips-textmate-sencha-touch-bundles/">Tips – TextMate 的 Sencha Touch bundles</a>
						</li>
					<li>
				<a href="http://www.tfan.org/phonegap-bridge-principle-and-plugin/">PhoneGap 本机通信原理与插件系统</a>
						</li>
					<li>
				<a href="http://www.tfan.org/wechat-on-nodejs-and-mongodb/">使用 Node.js 和 MongoDB 开发高性能微信公众平台应用</a>
						</li>
					<li>
				<a href="http://www.tfan.org/using-xml2js-for-express-body-parser/">如何让 Node-express 支持 XML 形式的 POST 请求?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/install-activerecord-sqlserver-adapter-under-centos/">CentOS 下使 Rails 连接 SQL Server 数据库: 安装 activerecord-sqlserver-adapter gem 包</a>
						</li>
					<li>
				<a href="http://www.tfan.org/node-js-express-weixin-weilearning/">Node.js + Node-express 开发的一个微信公众平台应用 – 中文和多国语言互译</a>
						</li>
					<li>
				<a href="http://www.tfan.org/nodejs-running-on-port-80-with-apache/">在 CentOS 上使用 Apache 的 Proxy mode 使 Node.js 和 Apache 共用 80 端口</a>
						</li>
					<li>
				<a href="http://www.tfan.org/from-sqlserver-to-mongodb-using-ruby-and-rails/">把 SQL Server 导入 MongoDB 的 Ruby 脚本</a>
						</li>
					<li>
				<a href="http://www.tfan.org/update-adt-and-android-sdk-in-five-minutes/">三步升级已安装的 Android SDK 和 ADT 插件</a>
						</li>
					<li>
				<a href="http://www.tfan.org/tips-npm-install-express/">Tips: Node.js 在 Mac 下 npm 安装 express 失败</a>
						</li>
					<li>
				<a href="http://www.tfan.org/html5-device-access/">HTML5 设备访问入门</a>
						</li>
					<li>
				<a href="http://www.tfan.org/web-storage/">Web Storage</a>
						</li>
					<li>
				<a href="http://www.tfan.org/mobile-first/">Mobile First</a>
						</li>
					<li>
				<a href="http://www.tfan.org/html5-offline-web-application/">HTML5 离线功能详解 – 构建 Offline Web Application</a>
						</li>
					<li>
				<a href="http://www.tfan.org/manual-edit-filters-in-adblock/">Tips: 通过禁止访问被墙掉的链接来加快页面加载速度</a>
						</li>
					<li>
				<a href="http://www.tfan.org/mobile-app-development-options-which-way-to-go/">移动应用开发方式的选择: Native, Hybrid, 还是 Mobile Web ?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/android-dashboards/">官方的 Android 平台版本及屏幕大小市场占有率统计报告</a>
						</li>
					<li>
				<a href="http://www.tfan.org/the-latest-updates-to-jquery-mobile/">jQuery Mobile 的最新更新</a>
						</li>
					<li>
				<a href="http://www.tfan.org/indexeddb/">IndexedDB</a>
						</li>
					<li>
				<a href="http://www.tfan.org/pointer-lock-api/">Pointer Lock API</a>
						</li>
					<li>
				<a href="http://www.tfan.org/my-mdn-translation/">我的 MDN 文档中文化列表</a>
						</li>
					<li>
				<a href="http://www.tfan.org/sharing-wireless-connection-with-router-using-wirelesscard/">路由器共享无线上网卡上网</a>
						</li>
					<li>
				<a href="http://www.tfan.org/integrating-cocos2d-api-documents-into-xcode/">生成 Cocos2d API 文档并集成到 Xcode 中</a>
						</li>
					<li>
				<a href="http://www.tfan.org/build-a-simple-ssh-git-service-in-five-minutes/">快速架设一个使用 SSH 的 Git 服务</a>
						</li>
					<li>
				<a href="http://www.tfan.org/wordpress-on-linode-centos-6/">如何在 Linode 的 CentOS 6 上部署 WordPress 应用?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/using-aircrack-ng-under-mac/">如何在 MAC 下面使用 Aircrack-ng?</a>
						</li>
					<li>
				<a href="http://www.tfan.org/using-indexeddb/">使用 IndexedDB</a>
						</li>
					<li>
				<a href="http://www.tfan.org/no-setversion-now-onupgradeneeded/">没有 setVersion 了,现在是 onupgradeneeded !</a>
						</li>
					<li>
				<a href="http://www.tfan.org/going-deeper-with-jquery-mobile/">深入了解 jQuery Mobile</a>
						</li>
					<li>
				<a href="http://www.tfan.org/working-off-the-grid-html5-offline/">Working Off the Grid: HTML5 离线应用</a>
						</li>
					<li>
				<a href="http://www.tfan.org/spdy-and-the-road-towards-http20/">SPDY 和通向 HTTP 2.0 之路</a>
						</li>
					<li>
				<a href="http://www.tfan.org/cocos2d-fourinch/">给 Cocos2d 增加对 4 英寸屏幕的支持 (iPhone 5, iPod touch 5 代)</a>
						</li>
					<li>
				<a href="http://www.tfan.org/html5-slides/">HTML5 Web 开发培训讲义</a>
						</li>
					<li>
				<a href="http://www.tfan.org/ios-programming-slides/">iOS Programming  笔记(第三版)</a>
						</li>
					<li>
				<a href="http://www.tfan.org/video_what_is_html5/">小短片:什么是 HTML5 ?</a>
						</li>
				</ul>
		</div> <!-- end .et_pb_widget -->	</div> <!-- end #sidebar -->
		</div> <!-- #content-area -->
	</div> <!-- .container -->
</div> <!-- #main-content -->


	<span class="et_pb_scroll_top et-pb-icon"></span>


			<footer id="main-footer">
				

		
				<div id="footer-bottom">
					<div class="container clearfix">
				
						<p id="footer-info">由 <a href="http://www.elegantthemes.com" title="Premium WordPress Themes">Elegant Themes</a> 设计 |  <a href="http://www.wordpress.org">WordPress</a> 提供技术支持</p>
					</div>	<!-- .container -->
				</div>
			</footer> <!-- #main-footer -->
		</div> <!-- #et-main-area -->


	</div> <!-- #page-container -->

	<script type='text/javascript' src='http://www.tfan.org/wp-content/plugins/akismet/_inc/form.js?ver=3.1.3'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var tocplus = {"visibility_show":"\u663e\u793a","visibility_hide":"\u9690\u85cf","width":"Auto"};
/* ]]> */
</script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/plugins/table-of-contents-plus/front.js?ver=1505'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-includes/js/comment-reply.min.js?ver=4.3'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/themes/Divi/js/jquery.fitvids.js?ver=2.4.6.1'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/themes/Divi/js/waypoints.min.js?ver=2.4.6.1'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/themes/Divi/js/jquery.magnific-popup.js?ver=2.4.6.1'></script>
<script type='text/javascript'>
/* <![CDATA[ */
var et_custom = {"ajaxurl":"http:\/\/www.tfan.org\/wp-admin\/admin-ajax.php","images_uri":"http:\/\/www.tfan.org\/wp-content\/themes\/Divi\/images","builder_images_uri":"http:\/\/www.tfan.org\/wp-content\/themes\/Divi\/includes\/builder\/images","et_load_nonce":"0dc48ee82a","subscription_failed":"\u8bf7\u68c0\u67e5\u4e0b\u9762\u7684\u8f93\u5165\u5b57\u6bb5\uff0c\u4ee5\u786e\u4fdd\u60a8\u8f93\u5165\u4e86\u6b63\u786e\u7684\u4fe1\u606f\u3002","fill":"\u586b\u5199","field":"\u5b57\u6bb5","invalid":"\u65e0\u6548\u7684\u7535\u5b50\u90ae\u4ef6","captcha":"\u9a8c\u8bc1\u7801","prev":"\u4e0a\u4e00\u9875","previous":"\u4e0a\u4e00\u9875","next":"\u4e0b\u4e00\u9875"};
/* ]]> */
</script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/themes/Divi/js/custom.js?ver=2.4.6.1'></script>
<script type='text/javascript' src='http://www.tfan.org/wp-content/themes/Divi/js/smoothscroll.js?ver=2.4.6.1'></script>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值