Chrome目前不支持onupgradeneeded事件。如果你想使用旧的或新的W3规范去的ObjectStore的参考,这是利用旧setVersion命令,或者通过新的onupgradeneeded事件通过的onSuccess事件一个可能的解决方法:
var ixDb;
var ixDbRequest;
var ixDbVersionTansaction;
//Check to see if we have a browser that supports IndexedDB
if (window.indexedDB) {
ixDbRequest = window.indexedDB.open(dbName, dbVersion);
//For browsers like chrome that support the old set version method
ixDbRequest.onsuccess = function (e) {
ixDb = ixDbRequest.result || e.result;
if (typeof ixDb.setVersion === "function") {
//Put your version checking logic here
if (oldVersion < newVersion) {
var verRequest = ixDb.setVersion(newVersion);
verRequest.onerror = function (e) {
//handling error logic here
}
verRequest.onsuccess = function (e) {
//Get a reference to the version transaction
//from the old setVersion method.
ixDbVersionTansaction = verRequest.result;
//Create database using function provided by the user.
UserFunction();
}
}
}
};
ixDbRequest.onupgradeneeded = function (e) {
//FF uses this event to fire the transaction for upgrades.
//All browsers will eventually use this method. Per - W3C Working Draft 24 May 2012
ixDb = ixDbRequest.result || e.currentTarget.result;
//Get a reference to the version transaction via the onupgradeneeded event (e)
ixDbVersionTansaction = e.currentTarget.transaction;
//Create database using function provided by the user.
UserFunction();
};
UserFunction(){
//ObjectStore is accessed via ixDbVersionTansaction variable
// in either instance (transaction..objectStore("ObjectStoreName"))
var ObjectStore = ixDbVersionTansaction.objectStore("ObjectStoreName");
var index = ObjectStore.createIndex("ixName", "fieldName");
}
request.onupgradeneeded =函数(evt)var objectStore = evt.currentTarget.transaction.objectStore(“people”); objectStore.createIndex(“city”,“city”,{unique:false}); }; //快速回答 – Garry 2016-04-01 00:03:36