IDBKeyRange:upperOpen 屬性

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

注意:此功能在 Web Workers 中可用。

upperOpenIDBKeyRange 介面的一個只讀屬性,返回一個布林值,指示上界值是否包含在鍵範圍中。

一個布林值。

指示
true 上界值不包含在鍵範圍中。
false 上界值包含在鍵範圍中。

示例

以下示例說明了如何使用鍵範圍。在此,我們宣告 keyRangeValue = IDBKeyRange.upperBound("F", "W", true, true); — 一個包含“F”和“W”之間所有內容但**不**包含它們本身的範圍 — 因為上下界都被宣告為開區間(true)。我們開啟一個事務(使用 IDBTransaction)和一個物件儲存,並使用 IDBObjectStore.openCursor 開啟一個遊標,將 keyRangeValue 宣告為其可選的鍵範圍值。

宣告鍵範圍後,我們將 upperOpen 屬性值記錄到控制檯,其值應顯示為“true”:上界是開放的,因此不包含在範圍內。

注意:有關一個更完整的示例,允許您嘗試鍵範圍,請檢視我們的 IDBKeyRange-example 倉庫(也可以 線上檢視示例)。

js
function displayData() {
  const keyRangeValue = IDBKeyRange.bound("F", "W", true, true);
  console.log(keyRangeValue.upperOpen);

  const transaction = db.transaction(["fThings"], "readonly");
  const objectStore = transaction.objectStore("fThings");
  objectStore.openCursor(keyRangeValue).onsuccess = (event) => {
    const cursor = event.target.result;
    if (cursor) {
      const listItem = document.createElement("li");
      listItem.textContent = `${cursor.value.fThing}, ${cursor.value.fRating}`;
      list.appendChild(listItem);
      cursor.continue();
    } else {
      console.log("Entries all displayed.");
    }
  };
}

規範

規範
Indexed Database API 3.0
# ref-for-dom-idbkeyrange-upperopen①

瀏覽器相容性

另見