IDBKeyRange.lowerBound()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2021.

IDBKeyRange 接口的 lowerBound() 方法创建一个只有下限的新的键范围。默认情况下,它包含较低的端点值。

备注: 此特性在 Web Worker 中可用。

语法

js
IDBKeyRange.lowerBound(lower)
IDBKeyRange.lowerBound(lower, open)

参数

lower

指定新键范围的下限。

open 可选

指示下限是否排除端点值。默认值为 false。

返回值

IDBKeyRange: 新创建的键范围。

异常

此方法可能引发以下类型的 DOMException :

Exception Description
DataError The value parameter passed was not a valid key.

示例

下面的示例演示如何使用下限键范围。在这里,我们声明keyRangeValue = IDBKeyRange.lowerBound("F", false);— 一个包含值“F”及其后所有内容的范围。我们打开一个事务(使用 IDBTransaction)和一个对象存储,并使用 IDBObjectStore.openCursor打开一个游标,声明keyRangeValue 为其可选的键范围值。这意味着光标将只检索键值为“F”及其后面的所有记录。如果使用IDBKeyRange.lowerBound("F", true);,则该范围将不包括端点“F”,仅包括其后面的值。

备注: 要获得一个更完整的示例,使你能够使用键范围进行实验,请查看我们的 IDBKeyRange-example实时查看该示例)。

js
function displayData() {
  var keyRangeValue = IDBKeyRange.lowerBound("F");

  var transaction = db.transaction(["fThings"], "readonly");
  var objectStore = transaction.objectStore("fThings");

  objectStore.openCursor(keyRangeValue).onsuccess = function (event) {
    var cursor = event.target.result;
    if (cursor) {
      var listItem = document.createElement("li");
      listItem.innerHTML =
        "<strong>" + cursor.value.fThing + "</strong>, " + cursor.value.fRating;
      list.appendChild(listItem);

      cursor.continue();
    } else {
      console.log("Entries all displayed.");
    }
  };
}

规范

Specification
Indexed Database API 3.0
# ref-for-dom-idbkeyrange-lowerbound①

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
lowerBound() static method

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见