• 事件触发条件: storage 事件只会在其他窗口或标签页修改 localStorage 时触发。在同一窗口中对 localStorage 的修改不会触发此事件。

  • 事件处理: 可以通过监听 window 对象的 storage 事件来处理:

    window.addEventListener('storage', function(event) {
      if (event.key === 'yourKey') {
        console.log('localStorage has changed:', event.newValue);
      }
    });