【发布时间】:2016-08-08 05:47:35
【问题描述】:
我有一个功能代码,它将类元素的标题添加到数组中,然后将其推送到 localStorage“数据库”。一切都很好,但我不想把元素推到那里很多次“如果它已经在那里”。尝试了几个解决方案,但没有完全得到它。非常感谢任何帮助。大脑正在融化..,
var stores=[]
jQuery('.additem').click(function() {
var x = jQuery(this).closest('h4.title').text();
if ((x == null) || (x == "") || (x == ($.inArray(this.x) > -1))) {
jQuery(this).closest('.infobox').html('Already there or empty.');
jQuery(this).closest('.infobox').show().delay(500).fadeOut(1000);
} else {
stores.push(x);
console.log(stores);
window.localStorage.setItem("database", stores.join(" + "));
}
});
也试过
(x == (stores.indexOf(this) > -1))
还有一些其他的。 谢谢
【问题讨论】:
标签: javascript arrays local-storage