Jquery 和 localStorage 如何向站点添加更多元素 clipdoard/cart
Jquery and localStorage how to add more elements to the site clipdoard/cart
我正在使用localStorage学习jQuery,并希望创建某种页面剪贴板选项卡,您可以在其中添加产品,图片等以供以后查看。
这是我的jquery代码:
var count = 0;
jQuery('.add_rem').click(function(e) {
e.preventDefault();
if(jQuery(this).hasClass('remove')) {
count -= 1;
}else {
count += 1;
}
jQuery.each( function(){
if(jQuery(this).hasClass('remove')){
if(count == 0) {
jQuery('.list li.empty').show();
}
if($(".remove").attr('id') == localStorage.getItem('id') ) {
localStorage.removeItem('id');
$("#"+localStorage.getItem('id')).removeClass('remove');
}
} else {
jQuery('.list li.empty').hide();
localStorage.setItem('count', count);
localStorage.setItem('id', jQuery(this).attr('id'));
if(jQuery(this).attr('id') == localStorage.getItem('id')){
jQuery(".add_rem#"+localStorage.getItem('id')).addClass("remove");
}
localStorage.setItem('link', jQuery('.link').attr('href'));
localStorage.setItem('photo', jQuery('.photo').attr('src'));
localStorage.setItem('name', jQuery('b.name').text());
jQuery('#hp-content .wrapper .list').append('<li><a href="'+ localStorage.getItem('link') +'"> <img src="'+ localStorage.getItem('photo') +'" alt="photo" /><br />'+ localStorage.getItem('name') +' </a></li>');
jQuery('.panel2 a span').text('('+ localStorage.getItem('count') +')');
}
jQuery('.panel2 a span').text('('+ localStorage.getItem('count') +')');
})
})
这是一个产品的 HTML 代码:
<tr>
<td class="nul">
<a href="javascript:;" class="add_rem"><img src="/img/check_of.png" alt="Add / remove from clipboard" title="add to clipboard"></a>
</td>
<td class="nul">
<a href="13/test/" class="link">
<img class="photo" alt="Test" src="images/pl/brak.gif">
</a>
</td>
<td class="nul"><b>Test1 / kk</b></td>
<td class="nul"><i>Princeton</i></td>
<td class="nul">College</td>
<td class="nul">
<a href="13/test/">
<img alt="Show" src="images/general/show.gif">
</a>
</td>
</tr>
这是剪贴板的 HTML:
<div style="" id="clipboard">
<div class="panels">
<ul>
<li class="panel2"><a href="#">Clipboard <span>(0)</span></a></li>
</ul>
</div>
<div id="hp-content"><div id="clipboard-bg">
<div class="wrapper">
<ul class="list">
<li class="empty"><strong>Empty.</strong><span class="one">The clipboard is now empty.</span></li>
</ul>
</div>
</div>
但是在页面重新加载后,所有复制的元素都会被删除
我有一个本地存储jQuery插件,你可以使用:http://plugins.jquery.com/tag/localstorage/。但是本地存储非常简单,您甚至不需要该插件。以下是我为您编写的一些函数:
请注意,这基本上是一个小型的javascript库:(LS = LocalStorage
)
(function(){
var LS = function(){
return new LS.fn.init();
};
LS.fn = LS.protorype ={
//Check to see if the browser suports LocalStorage
init : function(){
this.ls = (typeof(Storage)!=="undefined") ? false : true;
return this;
}
}
LS.fn.init.prototype = LS.fn;
LS.fn.init.prototype = {
set : function(name, val){
if(this.ls) localStorage.setItem(name, val);
},
get : function (name){
if(this.ls) return localStorage.getItem(name);
},
remove : function(name){
if(this.ls) localStorage.removeItem(name);
}
}
window.LS = window._ = LS;
})()
此库的小型文档。
设置:设置本地存储数据的值。前任:
LS().set("Foo", "bar");
--OR--
_().set("Foo", "bar");
获取
:获取本地存储数据的值。前任:
LS().get("Foo")//returns "bar"
--OR--
_().get("Foo")
删除:删除本地存储数据。前任:
LS().remove("Foo");
--or--
_().remove("Foo");
希望这有帮助!
祝你好运!
相关文章:
- CKEditor Widget-阻止编辑可编辑元素本身
- 如何设置html元素填充的动画
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 使用clickToggle并在单击另一个元素时关闭元素
- 单击时将焦点更改为元素
- 表追加而不附加最后一个元素
- 如何在jQuery中获取元素的形式
- 我可以获得相对于被点击元素的确切点击位置吗
- 在函数中添加数组元素的数值
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 将视口底部滚动到元素底部
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 如何使用jquery处理php循环通过元素
- Ckeditor-plugin:插入虚假元素add不情愿<p>标签前后
- JQuery添加元素需要在我的js之前再次添加JQuery脚本
- 在Jquery detachment()和appendTo()之后定位元素
- 在动态创建的元素上获取对特定选择器的引用
- Jquery 和 localStorage 如何向站点添加更多元素 clipdoard/cart