<div id="fonts"></div> <div class="content">字体切换</div>
$(function () { function SetCookie(name, value) { var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + decodeURI(value) + ";expires=" + exp.toGMTString(); } function getCookie(name) { var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)")); if (arr != null) return decodeURI(arr[2]); return null; } var kk = getCookie("fonts"); if (kk == "") { SetCookie("fonts", "lyhei") } else if (kk == "songti") { $(".content").css('font-family', '"宋体"'); $("#fonts").text('雅黑'); } else { $(".content").css('font-family', '"微软雅黑"'); $("#fonts").text('宋体'); } $("#fonts").click(function () { if ($("#fonts").text() == "宋体") { SetCookie("fonts", "songti"); $(".content").css('font-family', '"宋体"'); $("#fonts").text('雅黑'); } else { SetCookie("fonts", "lyhei"); $(".content").css('font-family', '"微软雅黑"') $("#fonts").text('宋体'); } }); });
Done!