[英]how to make use of local storage? (when the click function adds data-id to the class then the css color is changed)
I tried it with the jquery
code below but it can't be saved in localstorage
, is there a solution other than the method below?我用下面的
jquery
代码试过了,但是不能保存在localstorage
,除了下面的方法还有其他解决方案吗?
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<a class="abc" data-id="1">Attachments</a>
<a class="abc" data-id="2">Removal</a>
<a class="abc" data-id="3">Hair Loss</a>
JQuery查询
$(document).ready(function() {
$(".abc").click(function() {
var dataID = $(this).data('id');
var idAdd = $(this).addClass("id" + dataID);
$(idAdd).css({
"background": "#fcffd6",
"border": "3px solid #acdcd86b"
});
localStorage.setItem("someVarName", $(this).text());
});
if (localStorage.getItem("someVarName") != null) {
var dataID = $(this).data('id');
var clAD = $(this).addClass("id" + dataID);
$(clAD).filter(function() {
return this.innerHTML == localStorage.getItem("someVarName");
}).css({
"background": "#fcffd6",
"border": "3px solid #acdcd86b"
});
}
});
The code编码
var dataID = $(this).data('id'); var clAD = $(this).addClass("id" + dataID); debugger $(clAD).filter(function() { return this.innerHTML == localStorage.getItem("someVarName"); }).css({ "background": "#fcffd6", "border": "3px solid #acdcd86b" });
does not make any sense.没有任何意义。 What you want to do here is to enumerate and filter all a-elements:
您在这里要做的是枚举和过滤所有 a 元素:
$("a").filter(function() { return this.innerHTML == localStorage.getItem("someVarName"); }).css({ "background": "#fcffd6", "border": "3px solid #acdcd86b" });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.