admin管理员组文章数量:1200357
How can you remove a class name and replace it with a new one?
<style>
.red {background-color: #FF0000;}
.green{background-color: #00FF00;}
.blue {background-color: #0000FF;}
</style>
<body class="red">
<ul>
<li><a href="">red</a></li>
<li><a href="">green</a></li>
<li><a href="">blue</a></li>
</ul>
</body>
In this case when you click on red or green or blue, the body class name will change accordingly. Also it will make a cookie which will save the choice.
I tried the jQuery .addClass and it's working, but it's adding a class on top of the existing one. Also I can't manage to save the cookie.
How can you remove a class name and replace it with a new one?
<style>
.red {background-color: #FF0000;}
.green{background-color: #00FF00;}
.blue {background-color: #0000FF;}
</style>
<body class="red">
<ul>
<li><a href="">red</a></li>
<li><a href="">green</a></li>
<li><a href="">blue</a></li>
</ul>
</body>
In this case when you click on red or green or blue, the body class name will change accordingly. Also it will make a cookie which will save the choice.
I tried the jQuery .addClass and it's working, but it's adding a class on top of the existing one. Also I can't manage to save the cookie.
Share Improve this question edited Jan 8, 2018 at 13:13 Peter Mortensen 31.6k22 gold badges109 silver badges133 bronze badges asked Apr 29, 2009 at 22:37 kakkalokakkalo 8253 gold badges8 silver badges11 bronze badges 03 Answers
Reset to default 18Use:
$('.red').removeClass('red').addClass('blue');
Here's the full working code:
$(function() {
$("a").click(function() {
var color = $(this).text();
$("body").removeClass().addClass(color);
return false;
});
});
And now for the cookie part
$(function() {
$("a").click(function() {
var color = $(this).text();
$("body").removeClass().addClass(color);
createCookie("color",color);
return false;
});
if (readCookie("color") != null) {
$("body").removeClass().addClass(readCookie("color"));
}
else {
$("body").removeClass().addClass("red");
}
});
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else
var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ')
c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0)
return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
Working example here. A thank you to QuirksMode for the pre-made cookie code (cookie-cutter cookie code?)
Without jQuery you can do this with a line as simple as:
document.getElementsByTagName("body")[0].className = "red";
This effectively removes the class assigned to the element and replace it with whatever you want.
For cookies, PPK has a few simple methods to read/write/erase cookies.
createCookie("mySiteCookie", "red", 7); // Set the cookie to expire in 7 days.
readCookie("mySiteCookie"); // Should yield "red"
You'll also need removeClass.
版权声明:本文标题:javascript - How do I remove an existing class name and add a new one with jQuery and cookies? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738593138a2101633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论