Javascript addClass-本地存储问题
我在一个网站上工作,希望在localStorage中存储一个类,尽管我遇到了一些问题 以下是HTML:Javascript addClass-本地存储问题,javascript,jquery,local-storage,modernizr,Javascript,Jquery,Local Storage,Modernizr,我在一个网站上工作,希望在localStorage中存储一个类,尽管我遇到了一些问题 以下是HTML: <ul class="type"> <li id="1" class="current">Example 1</li> <li id="2">Example 2</li> </ul> 另外,是否有一种方法可以测试localStorage是否工作?这可以用一种简单的方法编写,请尝试下面的方法 HTML <ul
<ul class="type">
<li id="1" class="current">Example 1</li>
<li id="2">Example 2</li>
</ul>
另外,是否有一种方法可以测试
localStorage
是否工作?这可以用一种简单的方法编写,请尝试下面的方法
HTML
<ul class="type">
<li id="1">Example 1</li>
<li id="2">Example 2</li>
</ul>
<br />
<a href="javascript:void(0);">Check Storage</a>
CSS
li.current {
color: red;
}
演示JS
希望这对您有所帮助。这很有帮助,谢谢!如果元素具有
.current
,如何更改值?如果($('#2').hasClass('current')){$('body').append('yay!');},它会像一样吗。是否要更改本地存储的值?在这种情况下,只有单击“li”元素才能更改该值。
$(document).ready(function(){
$('.type li').on('click', function(event){
$('.type li').removeClass('current');
$(this).addClass('current');
if(Modernizr.localstorage){
window.localStorage['temp_type'] = $(this).attr('id'); /*Storing the id of the selected element*/
}
});
$('a').on('click', function(event){
alert(window.localStorage['temp_type']); /*Check the current storage*/
});
});
li.current {
color: red;
}