From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../web/api/idbobjectstore/opencursor/index.html | 171 +++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 files/zh-cn/web/api/idbobjectstore/opencursor/index.html (limited to 'files/zh-cn/web/api/idbobjectstore/opencursor') diff --git a/files/zh-cn/web/api/idbobjectstore/opencursor/index.html b/files/zh-cn/web/api/idbobjectstore/opencursor/index.html new file mode 100644 index 0000000000..764582ad9a --- /dev/null +++ b/files/zh-cn/web/api/idbobjectstore/opencursor/index.html @@ -0,0 +1,171 @@ +--- +title: IDBObjectStore.openCursor +slug: Web/API/IDBObjectStore/openCursor +translation_of: Web/API/IDBObjectStore/openCursor +--- +

{{ APIRef("IDBObjectStore") }}

+ +
+

{{domxref("IDBObjectStore")}} 的 openCursor() 方法 返回一个{{domxref("IDBRequest")}} 对象,并在一个单独的线程中,返回一个新的 {{domxref("IDBCursorWithValue")}} 对象。此方法目的是用一个游标来遍历一个对象存储空间。

+
+ +

若要确认此操作是否成功完成,请监听结果的 success 事件。

+ +

{{AvailableInWorkers}}

+ +

语法

+ +
var request = ObjectStore.openCursor(query, direction);
+ +

参数

+ +
+
query {{optional_inline}}
+
要查询的键或者 {{domxref("IDBKeyRange")}} 。如果传一个有效的键,则会默认为只包含此键的范围。如果此参数不传值,则默认为一个选择了该对象存储空间全部记录的键范围。
+
direction {{optional_inline}}
+
一个 {{domxref("IDBCursorDirection")}} 来告诉游标要移动的方向。有效的值有 "next" 、"nextunique" 、"prev" 和 "prevunique"。默认值是 "next"
+
+ +

返回

+ +

一个 {{domxref("IDBRequest")}} 对象,在此对象上触发与此操作相关的后续事件。

+ +

异常

+ +

此方法可能引起以下类型之一的 {{domxref("DOMException")}} :

+ + + + + + + + + + + + + + + + + + + + + + +
异常描述
InvalidStateError此 {{domxref("IDBObjectStore")}} 或{{domxref("IDBIndex")}} 已被删除。
TransactionInactiveError此 {{domxref("IDBObjectStore")}} 的事务处于非活动状态。
DataError指定的键或键范围无效。
+ +

例子

+ +

在下面这个简单的片段中,我们创建一个事务,检索一个对象存储空间,然后用一个游标去遍历该对象存储空间的所有记录:

+ +
var transaction = db.transaction("name", "readonly");
+var objectStore = transaction.objectStore("name");
+var request = objectStore.openCursor();
+request.onsuccess = function(event) {
+  var cursor = event.target.result;
+  if(cursor) {
+    // cursor.value 包含正在被遍历的当前记录
+    // 这里你可以对 result 做些什么
+    cursor.continue();
+  } else {
+    // 没有更多 results
+  }
+};
+
+
+ +

规范 

+ +
+
+ + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('IndexedDB', '#widl-IDBIndex-openCursor-IDBRequest-any-range-IDBCursorDirection-direction', 'openCursor()')}}{{Spec2('IndexedDB')}} 
{{SpecName("IndexedDB 2", "#dom-idbobjectstore-opencursor", "openCursor()")}}{{Spec2("IndexedDB 2")}} 
+ +

浏览器兼容性

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support23{{property_prefix("webkit")}}
+ 24
10 {{property_prefix("moz")}}
+ {{CompatGeckoDesktop("16.0")}}
10, partial157.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)Firefox OSIE PhoneOpera MobileSafari Mobile
Basic support4.4{{CompatGeckoMobile("22.0")}}1.0.11022{{CompatNo}}
+
+ +

另请参阅

+ + -- cgit v1.2.3-54-g00ecf