aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbcursor/key/index.html
blob: 1a20f28dc4273815ef9056b8a8988c5728743c47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: IDBCursor.key
slug: Web/API/IDBCursor/key
translation_of: Web/API/IDBCursor/key
---
<p>{{APIRef("IndexedDB")}}</p>

<div>
<p><strong>key</strong>是只读属性,返回在游标中的位置。如果游标在范围之外,这个值会被置为undefined。游标的key可以是任何数据类型。</p>

<p>{{AvailableInWorkers}}</p>
</div>

<h2 id="语法">语法</h2>

<pre class="syntaxbox" style="font-size: 14px;">var <em>key = cursor</em>.key;</pre>

<h3 id="值"></h3>

<p>任意类型</p>

<h2 id="示例">示例</h2>

<p>在该示例中,我们创建一个事务,检索一个存储对象,然后使用游标遍历所有存储在object store 中的记录。遍历的过程中,我们把类似(相簿标题,这是我们的键key),游标的key打印出来。</p>

<p>我们可以不根据游标的key来选取数据;我们可以抓取所有。还要注意,在循环的每个迭代中,您可以使用cursor.value.foo从当前记录下获取数据。完整示例,请看<span style="line-height: 1.5;"><a href="https://github.com/mdn/IDBcursor-example/">IDBCursor example</a></span><span style="line-height: 1.5;"> (</span><a href="http://mdn.github.io/IDBcursor-example/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></p>

<pre style="font-size: 14px;"><span style="line-height: 1.5;">function displayData() {</span>
  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.');
    }
  };
};</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">说明</th>
  </tr>
  <tr>
   <td>{{SpecName('IndexedDB', '#widl-IDBCursor-key', 'key')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("IndexedDB 2", "#dom-idbcursor-key", "key")}}</td>
   <td>{{Spec2("IndexedDB 2")}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

{{Compat("api.IDBCursor.key")}}

<h2 id="相关链接">相关链接</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
 <li>Starting transactions: {{domxref("IDBDatabase")}}</li>
 <li>Using transactions: {{domxref("IDBTransaction")}}</li>
 <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
 <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
 <li>Using cursors: {{domxref("IDBCursor")}}</li>
 <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>