{"id":1168,"date":"2019-09-30T12:54:21","date_gmt":"2019-09-30T04:54:21","guid":{"rendered":"https:\/\/freesilo.com\/?p=1168"},"modified":"2019-09-30T12:54:21","modified_gmt":"2019-09-30T04:54:21","slug":"%e5%b8%b8%e7%94%a8jquery%e3%80%81javascript%e6%96%b9%e6%b3%95%e5%90%88%e9%9b%86","status":"publish","type":"post","link":"https:\/\/freesilo.com\/?p=1168","title":{"rendered":"\u5e38\u7528jquery\u3001javascript\u65b9\u6cd5\u5408\u96c6"},"content":{"rendered":"<h3 id=\"1-javascript\">1. javascript<\/h3>\n<ul>\n<li>\u683c\u5f0f\u5316\u65f6\u95f4\u6233\uff0c\u8fd4\u56de\u51e0\uff08\u521a\u521a\u3001\u5206\u949f\u3001\u5c0f\u65f6\u3001\u5929\uff09\u524d\u5b57\u6837<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction timeago(dateTimeStamp){\r\n    var minute = 1000 * 60;\r\n    var hour = minute * 60;\r\n    var day = hour * 24;\r\n    var week = day * 7;\r\n    var halfamonth = day * 15;\r\n    var month = day * 30;\r\n    var now = new Date().getTime();\r\n    var diffValue = now - dateTimeStamp;\r\n\r\n    if(diffValue &lt; 0){ return &quot;&quot;; } var minC = diffValue\/minute; var hourC = diffValue\/hour; var dayC = diffValue\/day; var weekC = diffValue\/week; var monthC = diffValue\/month; if(monthC &gt;= 1 &amp;&amp; monthC &lt;= 3){ result = &quot; &quot; + parseInt(monthC) + &quot;\u6708\u524d&quot;; }else if(weekC &gt;= 1 &amp;&amp; weekC &lt;= 3){ result = &quot; &quot; + parseInt(weekC) + &quot;\u5468\u524d&quot;; }else if(dayC &gt;= 1 &amp;&amp; dayC &lt;= 6){ result = &quot; &quot; + parseInt(dayC) + &quot;\u5929\u524d&quot;; }else if(hourC &gt;= 1 &amp;&amp; hourC &lt;= 23){ result = &quot; &quot; + parseInt(hourC) + &quot;\u5c0f\u65f6\u524d&quot;; }else if(minC &gt;= 1 &amp;&amp; minC &lt;= 59){ result =&quot; &quot; + parseInt(minC) + &quot;\u5206\u949f\u524d&quot;; }else if(diffValue &gt;= 0 &amp;&amp; diffValue &lt;= minute){\r\n        result = &quot;\u521a\u521a&quot;;\r\n    }else {\r\n        var datetime = new Date();\r\n        datetime.setTime(dateTimeStamp);\r\n        var Nyear = datetime.getFullYear();\r\n        var Nmonth = datetime.getMonth() + 1 &lt; 10 ? &quot;0&quot; + (datetime.getMonth() + 1) : datetime.getMonth() + 1;\r\n        var Ndate = datetime.getDate() &lt; 10 ? &quot;0&quot; + datetime.getDate() : datetime.getDate();\r\n        var Nhour = datetime.getHours() &lt; 10 ? &quot;0&quot; + datetime.getHours() : datetime.getHours();\r\n        var Nminute = datetime.getMinutes() &lt; 10 ? &quot;0&quot; + datetime.getMinutes() : datetime.getMinutes();\r\n        var Nsecond = datetime.getSeconds() &lt; 10 ? &quot;0&quot; + datetime.getSeconds() : datetime.getSeconds();\r\n        result = Nyear + &quot;-&quot; + Nmonth + &quot;-&quot; + Ndate;\r\n    }\r\n    return result;\r\n}\r\n<\/pre>\n<ul>\n<li>\u683c\u5f0f\u5316\u6587\u4ef6\u5927\u5c0f<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nfunction getFileSize(fileByte) {\r\n    var fileSizeByte = fileByte;\r\n    var fileSizeMsg = &quot;&quot;;\r\n    if (fileSizeByte &lt; 1048576) fileSizeMsg = (fileSizeByte \/ 1024).toFixed(2) + &quot; KB&quot;; else if (fileSizeByte == 1048576) fileSizeMsg = &quot;1MB&quot;; else if (fileSizeByte &gt; 1048576 &amp;&amp; fileSizeByte &lt; 1073741824) fileSizeMsg = (fileSizeByte \/ (1024 * 1024)).toFixed(2) + &quot; MB&quot;; else if (fileSizeByte &gt; 1048576 &amp;&amp; fileSizeByte == 1073741824) fileSizeMsg = &quot;1GB&quot;;\r\n    else if (fileSizeByte &gt; 1073741824 &amp;&amp; fileSizeByte &lt; 1099511627776) fileSizeMsg = (fileSizeByte \/ (1024 * 1024 * 1024)).toFixed(2) + &quot; GB&quot;;\r\n    else fileSizeMsg = &quot;\u6587\u4ef6\u8d85\u8fc71TB&quot;;\r\n    return fileSizeMsg;\r\n}\r\n<\/pre>\n<ul>\n<li>\u5b57\u7b26\u4e32\u8f6c\u65e5\u671f<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/**\r\n * \u5b57\u7b26\u4e32\u8f6c\u65e5\u671f\r\n * \u683c\u5f0f\uff1ayyyy-MM-dd HH:mm:ss\r\n *     yyyy-MM-dd\r\n * @param strDate\r\n * @returns {Date}\r\n *\/\r\nfunction strToDate(strDate){\r\n\tif(!strDate) return;\r\n\tvar o = new Date(strDate.replace(\/-\/g,&quot;\/&quot;));\r\n\treturn o;\r\n\t\r\n}\r\n<\/pre>\n<ul>\n<li>checkbox\u5355\u9009<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/**\r\n * checkbox\u5355\u9009\r\n * @param checkboxName\r\n * @param form\r\n *\/\r\nfunction singleCheckbox(checkboxName, form) {  \r\n    if (checkboxName == null) return;  \r\n    var f = form || document.forms&#x5B;0];  \r\n    checkboxs = document.getElementsByName(checkboxName);  \r\n      \r\n    for(i = 0; i &lt; checkboxs.length; i++){  \r\n        checkboxs&#x5B;i].onclick = function(){  \r\n            for (j = 0; j &lt; checkboxs.length; j++ ){  \r\n                if (this.value != checkboxs&#x5B;j].value &amp;&amp; checkboxs&#x5B;j].checked == true){  \r\n                    checkboxs&#x5B;j].checked = false;  \r\n                }  \r\n            }  \r\n        }  \r\n    }  \r\n}\r\n<\/pre>\n<ul>\n<li>\u6821\u9a8c\u53d8\u91cf\u503c\u662f\u5426\u4e3anull\u6216\u7a7a\u5b57\u7b26<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/**\r\n * \u6821\u9a8c\u53d8\u91cf\u503c\u662f\u5426\u4e3anull\u6216\u7a7a\u5b57\u7b26\uff08undefined\u3001null\u3001&quot;&quot; \u8fd4\u56defalse,\u5176\u4ed6\u8fd4\u56detrue\uff09\r\n *\/\r\nfunction isNotBlank(value){\r\n\tif(value == undefined){\r\n\t\treturn false;\r\n\t}\r\n\tif(value == &quot;undefined&quot;){\r\n\t\treturn false;\r\n\t}\r\n\tif(value == null){\r\n\t\treturn false;\r\n\t}\r\n\tif(value == &quot;null&quot;){\r\n\t\treturn false;\r\n\t}\r\n\tif(value === &quot;&quot;){\r\n\t\treturn false;\r\n\t}\r\n\treturn true;\r\n}\r\n<\/pre>\n<h3 id=\"2-JQuery\">2. JQuery<\/h3>\n<ul>\n<li>\u5c06Form\u8868\u5355\u5e8f\u5217\u5316\u6210JSON\u5bf9\u8c61<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$.fn.serializeObject = function() {\r\n\tvar o = {};\r\n\tvar a = this.serializeArray();\r\n\t$.each(a, function() {\r\n\t\tif (o&#x5B;this.name]) {\r\n\t\t\tif (!o&#x5B;this.name].push) {\r\n\t\t\t\to&#x5B;this.name] = &#x5B; o&#x5B;this.name] ];\r\n\t\t\t}\r\n\t\t\to&#x5B;this.name].push(this.value || '');\r\n\t\t} else {\r\n\t\t\to&#x5B;this.name] = this.value || '';\r\n\t\t}\r\n\t});\r\n\treturn o;\r\n};\r\n<\/pre>\n<ul>\n<li>JSON\u7ed9\u8868\u5355\u57df\u8d4b\u503c\uff0c\u7528\u4e8e\u7f16\u8f91\u548c\u67e5\u770b<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n$.extend({\r\n\t\/**\r\n\t * \u88c5\u8f7d\u8868\u5355\u6570\u636e jsonStr json\u6570\u636e\u683c\u5f0f\r\n\t *\/\r\n\tloadFormData : function(jsonStr) {\r\n\t\tvar obj = eval(&quot;(&quot; + jsonStr + &quot;)&quot;);\r\n\t\tvar key, value, tagName, type, arr;\r\n\t\tfor (x in obj) {\r\n\t\t\tkey = x;\r\n\t\t\tvalue = obj&#x5B;x];\r\n\r\n\t\t\t$(&quot;&#x5B;name='&quot; + key + &quot;'],&#x5B;name='&quot; + key + &quot;&#x5B;]']&quot;).each(function() {\r\n\t\t\t\ttagName = $(this)&#x5B;0].tagName;\r\n\t\t\t\ttype = $(this).attr('type');\r\n\t\t\t\tif (tagName == 'INPUT') {\r\n\t\t\t\t\tif (type == 'radio') {\r\n\t\t\t\t\t\t$(this).attr('checked', $(this).val() == value);\r\n\t\t\t\t\t} else if (type == 'checkbox') {\r\n\t\t\t\t\t\tarr = value.split(',');\r\n\t\t\t\t\t\tfor (var i = 0; i &lt; arr.length; i++) {\r\n\t\t\t\t\t\t\tif ($(this).val() == arr&#x5B;i]) {\r\n\t\t\t\t\t\t\t\t$(this).attr('checked', true);\r\n\t\t\t\t\t\t\t\tbreak;\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\t$(this).val(value);\r\n\t\t\t\t\t}\r\n\t\t\t\t} else if (tagName == 'SELECT' || tagName == 'TEXTAREA') {\r\n\t\t\t\t\t$(this).val(value);\r\n\t\t\t\t}\r\n\r\n\t\t\t});\r\n\t\t}\r\n\t}\r\n});\r\n<\/pre>\n<ul>\n<li>\u683c\u5f0f\u5316\u65e5\u671f<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/**\r\n* \u683c\u5f0f\u5316\u65e5\u671f \r\n* \u8c03\u7528\u65b9\u5f0f\uff1a var str = new Date().Format(&quot;yyyy-MM-dd HH:mm:ss&quot;);\r\n*\/\r\nDate.prototype.Format = function (fmt) { \/\/author: meizz  \r\n   var o = { \r\n       &quot;M+&quot;: this.getMonth() + 1, \/\/\u6708\u4efd  \r\n       &quot;d+&quot;: this.getDate(), \/\/\u65e5  \r\n       &quot;H+&quot;: this.getHours(), \/\/\u5c0f\u65f6  \r\n       &quot;m+&quot;: this.getMinutes(), \/\/\u5206  \r\n       &quot;s+&quot;: this.getSeconds(), \/\/\u79d2  \r\n       &quot;q+&quot;: Math.floor((this.getMonth() + 3) \/ 3), \/\/\u5b63\u5ea6  \r\n       &quot;S&quot;: this.getMilliseconds() \/\/\u6beb\u79d2  \r\n   }; \r\n   if (\/(y+)\/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + &quot;&quot;).substr(4 - RegExp.$1.length)); \r\n   for (var k in o) \r\n       if (new RegExp(&quot;(&quot; + k + &quot;)&quot;).test(fmt)) \r\n           fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o&#x5B;k]) : ((&quot;00&quot; + o&#x5B;k]).substr((&quot;&quot; + o&#x5B;k]).length))); \r\n   return fmt; \r\n} ;\r\n<\/pre>\n<ul>\n<li>\u6570\u7ec4<\/li>\n<\/ul>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/**\r\n * \u5220\u9664\u6570\u7ec4\u5143\u7d20\r\n *\/\r\nArray.prototype.remove=function(dx){ \r\n    if(isNaN(dx)||dx&gt;this.length){return false;} \r\n    for(var i=0,n=0;i&lt;this.length;i++) \r\n    { \r\n        if(this&#x5B;i]!=this&#x5B;dx]) \r\n        { \r\n            this&#x5B;n++]=this&#x5B;i] \r\n        } \r\n    } \r\n    this.length-=1 \r\n}\r\n\/\/\u68c0\u67e5\u662f\u4e0d\u662f\u6570\u7ec4\r\nfunction isArray(arg){\r\n  return Object.prototype.toString.call(arg) === '&#x5B;object Array]';\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1. javascript \u683c\u5f0f\u5316\u65f6\u95f4\u6233\uff0c\u8fd4\u56de\u51e0\uff08\u521a\u521a\u3001\u5206\u949f\u3001\u5c0f\u65f6\u3001\u5929\uff09\u524d\u5b57\u6837 &#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/freesilo.com\/?p=1168\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-1168","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1168"}],"version-history":[{"count":3,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1168\/revisions"}],"predecessor-version":[{"id":1208,"href":"https:\/\/freesilo.com\/index.php?rest_route=\/wp\/v2\/posts\/1168\/revisions\/1208"}],"wp:attachment":[{"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/freesilo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}