循环遍历包含关键字的 localStorage
有没有办法根据关键字在 javascript 或 JQuery 中循环 localStorage,而不是循环遍历所有 localStorage?我知道我能做
for (var i = 0; i < localStorage.length; i++)...
但我宁愿做类似的事情
$.each('localStorage:contains("keyword")')...
Is there a way to loop through localStorage in javascript or JQuery based on a keyword rather than looping through all of localStorage? I know I can do
for (var i = 0; i < localStorage.length; i++)...
But I'd rather do something like
$.each('localStorage:contains("keyword")')...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不能直接对通用对象使用 DOM 选择器。
但是,您可以创建一个根据传递的函数进行过滤的小函数:
然后您可以执行以下操作:
使用此通用函数,您可以构建其他便利函数,例如:
然后按照您想要的方式迭代
localStorage
就像这样简单:It's not directly possible to use DOM selectors for generic objects.
You could, however, create a little function that filters as per a passed function:
Then you can do:
Using this generic function you can build additional convenience functions like:
Then to iterate
localStorage
the way you want is as easy as:JQuery 有一个 inArray() 方法,我认为这就是您正在寻找的 -
JQuery inArray
在你的情况下,你可能需要这样的东西...
var index = jQuery.inArray("keyword", localStorage);
// 从这里用 localStorage[index] 做你需要做的事情
JQuery has an inArray() method and I think that's what you're looking for -
JQuery inArray
In your case, you probably need something like this ...
var index = jQuery.inArray("keyword", localStorage);
// do what you need to with localStorage[index] from here