key是只读属性,返回在游标中的位置。如果游标在范围之外,这个值会被置为undefined。游标的key可以是任何数据类型。
Note: 此特性在 Web Worker 中可用。
语法
var key = cursor.key;
值
任意类型
示例
在该示例中,我们创建一个事务,检索一个存储对象,然后使用游标遍历所有存储在object store 中的记录。遍历的过程中,我们把类似(相簿标题,这是我们的键key),游标的key打印出来。
我们可以不根据游标的key来选取数据;我们可以抓取所有。还要注意,在循环的每个迭代中,您可以使用cursor.value.foo从当前记录下获取数据。完整示例,请看IDBCursor example (view example live.)
function displayData() {
var transaction = db.transaction(['rushAlbumList'], "readonly");
var objectStore = transaction.objectStore('rushAlbumList');
objectStore.openCursor().onsuccess = function(event) {
var cursor = event.target.result;
if(cursor) {
var listItem = document.createElement('li');
listItem.innerHTML = cursor.value.albumTitle + ', ' + cursor.value.year;
list.appendChild(listItem);
console.log(cursor.key);
cursor.continue();
} else {
console.log('Entries all displayed.');
}
};
};
规范
规范 | 状态 | 说明 |
---|---|---|
Indexed Database API key |
Recommendation | |
Indexed Database API 2.0 key |
Recommendation |
浏览器兼容性
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 23webkit 24 |
(Yes) | 10 moz 16.0 (16.0) |
10, partial | 15 | 7.1 |
Available in workers | (Yes) | (Yes) | 37.0 (37.0) | ? | (Yes) | ? |
Binary keys | ? | ? | 51.0 (51.0) | ? | ? | ? |
Indexed Database 2.0 | 58 | ? | ? | ? | 45 | ? |
Feature | Android Webview | Chrome for Android | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | 22.0 (22.0) | 10 | 22 | 8 |
Available in workers | (Yes) | (Yes) | (Yes) | 37.0 (37.0) | ? | (Yes) | ? |
Binary keys | ? | ? | ? | 51.0 (51.0) | ? | ? | ? |
Indexed Database 2.0 | 58 | 58 | ? | ? | ? | 45 | ? |
相关链接
- Using IndexedDB
- Starting transactions:
IDBDatabase
- Using transactions:
IDBTransaction
- Setting a range of keys:
IDBKeyRange
- Retrieving and making changes to your data:
IDBObjectStore
- Using cursors:
IDBCursor
- Reference example: To-do Notifications (view example live.)