{"id":650,"date":"2009-01-05T16:01:31","date_gmt":"2009-01-05T08:01:31","guid":{"rendered":"http:\/\/fdream.net\/blog\/article\/650.aspx"},"modified":"2009-01-05T17:01:45","modified_gmt":"2009-01-05T09:01:45","slug":"Mootools%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95%e6%89%a9%e5%b1%95%ef%bc%88%e4%b8%80%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fdream.net\/blog\/article\/650","title":{"rendered":"Mootools\u5e38\u7528\u65b9\u6cd5\u6269\u5c55\uff08\u4e00\uff09"},"content":{"rendered":"<p>\u6536\u96c6\u4e86\u6211\u4eec\u56e2\u961f\u90e8\u5206\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u6211\u7a0d\u5fae\u8fdb\u884c\u4e86\u4e00\u4e0b\u6574\u7406\uff0c\u8d34\u51fa\u6765\u548c\u5927\u5bb6\u5206\u4eab\u3002<\/p>\n<p>\u5148\u4eceString\u7684\u6269\u5c55\u5f00\u59cb\u5427\uff0c\u540e\u9762\u6709\u4e00\u90e8\u5206\u7684\u6269\u5c55\u8981\u4f9d\u8d56\u8fd9\u91cc\u6269\u5c55\u7684\u65b9\u6cd5\u3002\u4e3a\u4e86\u66f4\u52a0\u6e05\u6670\u548c\u8be6\u7ec6\uff0c\u6211\u4f1a\u4e00\u4e2a\u65b9\u6cd5\u4e00\u4e2a\u65b9\u6cd5\u5730\u8d34\u51fa\u6765\uff0c\u4f60\u5b8c\u5168\u53ef\u4ee5\u628a\u6240\u6709\u7684\u65b9\u6cd5\u5408\u5e76\u5728\u4e00\u8d77\u3002<\/p>\n<h3>\u65b9\u6cd5\uff1astartsWith<\/h3>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4ee5\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u3002<\/p>\n<p><pre lang=\"javascript\">\nString.implement({\n    startsWith: function(s) {\n        \/\/\/&lt;summary&gt;\n        \/\/\/ \u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4ee5\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934\n        \/\/\/&lt;\/summary&gt;\n        \/\/\/&lt;param name=&quot;s&quot; type=&quot;String&quot;&gt;\u5f00\u59cb\u5b57\u7b26\u4e32&lt;\/param&gt;\n        \/\/\/&lt;returns type=&quot;BOOLEAN&quot; \/&gt;\n        return (this.indexOf(s) == 0);\n    }\n}\n<\/pre>\n<\/p>\n<h4>\u7b80\u5355\u793a\u4f8b\uff1a<\/h4>\n<p><pre lang=\"javascript\">\n\/\/ \u4e0b\u9762\u5c06\u8fd4\u56detrue\nvar a = 'here is a test string'.startsWith('here');\n\n\/\/ \u4e0b\u9762\u5c06\u8fd4\u56defalse\nvar b = 'here is a test string'.startsWith('hereis');\n<\/pre>\n<\/p>\n<h3>\u65b9\u6cd5\uff1atruncate<\/h3>\n<p>\u8bf4\u660e\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u5c06\u6309\u71672\u4e2a\u5b57\u7b26\u957f\u5ea6\u8ba1\u7b97\u3002<\/p>\n<p><pre lang=\"javascript\">\nString.implement({\n    truncate: function(len, ae) {\n        \/\/\/&lt;summary&gt;\n        \/\/\/ \u6309\u6307\u5b9a\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32\n        \/\/\/ \u53ef\u4ee5\u9009\u62e9\u5728\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u540e\u9762\u6dfb\u52a0\u6216\u8005\u4e0d\u6dfb\u52a0\u7701\u7565\u53f7\uff08\u4e09\u4e2a\u70b9\uff09\n        \/\/\/&lt;\/summary&gt;\n        \/\/\/&lt;param name=&quot;len&quot; type=&quot;Number&quot;&gt;\u8981\u622a\u53d6\u7684\u5b57\u7b26\u4e32\u957f\u5ea6&lt;\/param&gt;\n        \/\/\/&lt;param name=&quot;ae&quot; type=&quot;Boolean&quot;&gt;true\u6dfb\u52a0\u7701\u7565\u53f7\uff0cfalse\u4e0d\u6dfb\u52a0&lt;\/param&gt;\n        \/\/\/&lt;returns type=&quot;STRING&quot; \/&gt;\n        var tl = 0, ts = [];\n        var tt = this.length;\n        for (var i = 0; i &lt; tt; i++) {\n            if (this.charCodeAt(i) &gt; 255) {\n                tl += 2;\n            }\n            else {\n                tl++;\n            }\n\n            if (tl &gt; len) {\n                break;\n            }\n        }\n        return (ae &amp;&amp; i &lt; tt) ? this.substring(0, i) + '...' : this.substring(0, i);\n    }\n}\n<\/pre>\n<\/p>\n<h4>\u7b80\u5355\u793a\u4f8b\uff1a<\/h4>\n<p><pre lang=\"javascript\">\n\/\/ \u4e0b\u9762\u5c06\u8fd4\u56de 'here is a '\nvar a = 'here is a test string'.truncate('10');\n\n\/\/ \u4e0b\u9762\u5c06\u8fd4\u56de 'here is a ...'\nvar b = 'here is a test string'.truncate('10', true);\n<\/pre>\n<\/p>\n<h3>\u65b9\u6cd5\uff1aescapseHTML<\/h3>\n<p>\u8bf4\u660e\uff1a\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7684HTML\u4ee3\u7801\u8fdb\u884c\u8f6c\u4e49\u3002<\/p>\n<p><pre lang=\"javascript\">\nString.implement({\n    escapseHTML: function() {\n        \/\/\/&lt;summary&gt;\n        \/\/\/ \u5bf9\u5b57\u7b26\u4e2d\u7684HTML\u4ee3\u7801\u8fdb\u884c\u8f6c\u4e49\n        \/\/\/&lt;\/summary&gt;\n        \/\/\/&lt;returns type=&quot;STRING&quot; \/&gt;\n        var htmlChars = ['&amp;~&amp;', '&lt;~&lt;', '&gt;~&gt;'];\n        var r;\n        var s = this;\n        for (var i = 0; i &lt; htmlChars.length; i++) {\n            r = htmlChars[i].split('~');\n            s = s.replace(new RegExp(r[0], 'g'), r[1]);\n        }\n\n        return s;\n    }\n}\n<\/pre>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6536\u96c6\u4e86\u6211\u4eec\u56e2\u961f\u90e8\u5206\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u6211\u7a0d\u5fae\u8fdb\u884c\u4e86\u4e00\u4e0b\u6574\u7406\uff0c\u8d34\u51fa\u6765\u548c\u5927\u5bb6\u5206\u4eab\u3002 \u5148\u4eceString\u7684\u6269\u5c55\u5f00\u59cb\u5427\uff0c\u540e\u9762\u6709\u4e00\u90e8\u5206\u7684\u6269\u5c55\u8981\u4f9d\u8d56\u8fd9\u91cc\u6269\u5c55\u7684\u65b9\u6cd5\u3002\u4e3a\u4e86\u66f4\u52a0\u6e05\u6670\u548c\u8be6\u7ec6\uff0c\u6211\u4f1a\u4e00\u4e2a\u65b9\u6cd5\u4e00\u4e2a\u65b9\u6cd5\u5730\u8d34\u51fa\u6765\uff0c\u4f60\u5b8c\u5168\u53ef\u4ee5\u628a\u6240\u6709\u7684\u65b9\u6cd5\u5408\u5e76\u5728\u4e00\u8d77\u3002 \u65b9\u6cd5\uff1astartsWith \u8bf4\u660e\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u53ef\u4ee5\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4ee5\u6307\u5b9a\u7684\u5b57\u7b26\u4e32\u5f00\u5934\u3002 String.implement({ startsWith: function(s) { \/\/\/&lt;summary&gt; \/\/\/ \u68c0\u67e5\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u4e0d\u662f\u4ee5\u67d0\u4e2a\u5b57\u7b26\u4e32\u5f00\u5934 \/\/\/&lt;\/summary&gt; \/\/\/&lt;param name=&quot;s&quot; type=&quot;String&quot;&gt;\u5f00\u59cb\u5b57\u7b26\u4e32&lt;\/param&gt; \/\/\/&lt;returns type=&quot;BOOLEAN&quot; \/&gt; return (this.indexOf(s) == 0); } } \u7b80\u5355\u793a\u4f8b\uff1a \/\/ \u4e0b\u9762\u5c06\u8fd4\u56detrue var a = &#8216;here is a test string&#8217;.startsWith(&#8216;here&#8217;); \/\/ \u4e0b\u9762\u5c06\u8fd4\u56defalse var b = &#8216;here is a test string&#8217;.startsWith(&#8216;hereis&#8217;); \u65b9\u6cd5\uff1atruncate \u8bf4\u660e\uff1a\u8fd9\u4e2a\u65b9\u6cd5\u4f1a\u622a\u53d6\u6307\u5b9a\u957f\u5ea6\u7684\u5b57\u7b26\u4e32\uff0c\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u5c06\u6309\u71672\u4e2a\u5b57\u7b26\u957f\u5ea6\u8ba1\u7b97\u3002 String.implement({ truncate: function(len, ae) { \/\/\/&lt;summary&gt; \/\/\/ \u6309\u6307\u5b9a\u957f\u5ea6\u622a\u53d6\u5b57\u7b26\u4e32 &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[120,104,328],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-coding","tag-AJAX","tag-JavaScript","tag-mootools"],"views":7878,"_links":{"self":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/650","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/comments?post=650"}],"version-history":[{"count":0,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"wp:attachment":[{"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fdream.net\/blog\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}