--- 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);
"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
}
};
| Specification | Status | Comment |
|---|---|---|
| {{SpecName('IndexedDB', '#widl-IDBIndex-openCursor-IDBRequest-any-range-IDBCursorDirection-direction', 'openCursor()')}} | {{Spec2('IndexedDB')}} | |
| {{SpecName("IndexedDB 2", "#dom-idbobjectstore-opencursor", "openCursor()")}} | {{Spec2("IndexedDB 2")}} |