jqueryで1回目と2回目を分けたいのですが、次のコードは何がおかしいか教えていただけませんでしょうか??
javascript
1(function($){ 2 3 // .btnをクリックしたときの機能だよ 4 $('body').on("click",'.btn',function(){ 5 6 // まずbodyに.accessをつけて 7 $.cookie("access",$('body').addClass('access')); 8 9 // 1回目(.accessなし)ならこのアラート 10 if($.cookie("access") == undefined) { 11 alert('1回目の.btnクリックだね。'); 12 13 // 2回目以降(.accessあり)ならこのアラート 14 } else { 15 alert('2回目以降ですよ。'); 16 } 17 18 }); 19 20})(jQuery);
javascript
1//ライブラリ「jquery.cookie.js」の中身全部 2(function($){ 3(function (factory) { 4 if (typeof define === 'function' && define.amd) { 5 // AMD (Register as an anonymous module) 6 define(['jquery'], factory); 7 } else if (typeof exports === 'object') { 8 // Node/CommonJS 9 module.exports = factory(require('jquery')); 10 } else { 11 // Browser globals 12 factory(jQuery); 13 } 14}(function ($) { 15 16 var pluses = /+/g; 17 18 function encode(s) { 19 return config.raw ? s : encodeURIComponent(s); 20 } 21 22 function decode(s) { 23 return config.raw ? s : decodeURIComponent(s); 24 } 25 26 function stringifyCookieValue(value) { 27 return encode(config.json ? JSON.stringify(value) : String(value)); 28 } 29 30 function parseCookieValue(s) { 31 if (s.indexOf('"') === 0) { 32 // This is a quoted cookie as according to RFC2068, unescape... 33 s = s.slice(1, -1).replace(/\"/g, '"').replace(/\\/g, '\'); 34 } 35 36 try { 37 // Replace server-side written pluses with spaces. 38 // If we can't decode the cookie, ignore it, it's unusable. 39 // If we can't parse the cookie, ignore it, it's unusable. 40 s = decodeURIComponent(s.replace(pluses, ' ')); 41 return config.json ? JSON.parse(s) : s; 42 } catch(e) {} 43 } 44 45 function read(s, converter) { 46 var value = config.raw ? s : parseCookieValue(s); 47 return $.isFunction(converter) ? converter(value) : value; 48 } 49 50 var config = $.cookie = function (key, value, options) { 51 52 // Write 53 54 if (arguments.length > 1 && !$.isFunction(value)) { 55 options = $.extend({}, config.defaults, options); 56 57 if (typeof options.expires === 'number') { 58 var days = options.expires, t = options.expires = new Date(); 59 t.setMilliseconds(t.getMilliseconds() + days * 864e+5); 60 } 61 62 return (document.cookie = [ 63 encode(key), '=', stringifyCookieValue(value), 64 options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE 65 options.path ? '; path=' + options.path : '', 66 options.domain ? '; domain=' + options.domain : '', 67 options.secure ? '; secure' : '' 68 ].join('')); 69 } 70 71 // Read 72 73 var result = key ? undefined : {}, 74 // To prevent the for loop in the first place assign an empty array 75 // in case there are no cookies at all. Also prevents odd result when 76 // calling $.cookie(). 77 cookies = document.cookie ? document.cookie.split('; ') : [], 78 i = 0, 79 l = cookies.length; 80 81 for (; i < l; i++) { 82 var parts = cookies[i].split('='), 83 name = decode(parts.shift()), 84 cookie = parts.join('='); 85 86 if (key === name) { 87 // If second argument (value) is a function it's a converter... 88 result = read(cookie, value); 89 break; 90 } 91 92 // Prevent storing a cookie that we couldn't decode. 93 if (!key && (cookie = read(cookie)) !== undefined) { 94 result[name] = cookie; 95 } 96 } 97 98 return result; 99 }; 100 101 config.defaults = {}; 102 103 $.removeCookie = function (key, options) { 104 // Must not alter options, thus extending a fresh object... 105 $.cookie(key, '', $.extend({}, options, { expires: -1 })); 106 return !$.cookie(key); 107 }; 108 109})); 110})(jQuery);
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2018/03/31 13:21 編集
2018/03/31 13:25
退会済みユーザー
2018/03/31 13:47
退会済みユーザー
2018/03/31 17:49 編集