{"id":31390,"date":"2024-11-25T12:54:36","date_gmt":"2024-11-25T04:54:36","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31390\/"},"modified":"2024-11-25T12:54:36","modified_gmt":"2024-11-25T04:54:36","slug":"js%e7%9a%84%e8%87%aa%e5%ae%9a%e4%b9%89trim%e5%87%bd%e6%95%b0%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31390\/","title":{"rendered":"js\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765js\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\uff0c\u4f7f\u7528js\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u517c\u5bb9IE\u4f4e\u7248\u672c\u6d4f\u89c8\u5668\uff0c\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4f4e\u7248\u672c\u811a\u672c\u7684\u6d4f\u89c8\u5668<\/p>\n<p style=\"text-align: left\"><strong>js\u4e2d\u672c\u8eab\u662f\u6ca1\u6709trim\u51fd\u6570\u7684<\/strong><\/p>\n<pre>\/\/\u5220\u9664\u5de6\u53f3\u4e24\u7aef\u7684\u7a7a\u683c\r\nfunction trim(str){\r\n return str.replace(\/(^s*)|(s*$)\/g, \"\");\r\n}\r\n\/\/\u5220\u9664\u5de6\u8fb9\u7684\u7a7a\u683c\r\nfunction ltrim(str){\r\n return str.replace(\/(^s*)\/g,\"\");\r\n}\r\n\/\/\u5220\u9664\u53f3\u8fb9\u7684\u7a7a\u683c\r\nfunction rtrim(str){\r\n return str.replace(\/(s*$)\/g,\"\");\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><strong>\u6216\u8005\u6269\u5c55String\u7c7b\u578b\u5c5e\u6027<\/strong><\/p>\n<pre>String.prototype.trim = function()\r\n{\r\n return this.replace(\/(^s*)|(s*$)\/g, \"\");\r\n}\r\nvar str = document.getElementById(\"test\").value;\r\nalert( str.trim() );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><strong>\u6216\u8005\u5e72\u8106\u653e\u5f03\u4f7f\u7528js,\u4f7f\u7528jQuer\u4e4b<\/strong>$.trim(str)<\/p>\n<pre>var str = $(\"#test\").val();\r\nalert( $.trim(str) );<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><span style=\"color: #800000\"><strong>PS\uff1a\u793a\u4f8b\u4e2d\u7528\u5230\u4e86\u6b63\u5219\u64cd\u4f5c\uff0c\u8fd9\u91cc\u4e3a\u5927\u5bb6\u63d0\u4f9b2\u6b3e\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\u4f9b\u5927\u5bb6\u53c2\u8003\u4f7f\u7528\uff1a<\/strong><\/span><\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff6600\"><strong><\/strong><\/span><\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p style=\"text-align: left\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fjs\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765js\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\uff0c\u4f7f\u7528js\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u517c\u5bb9IE\u4f4e\u7248\u672c\u6d4f\u89c8\u5668\uff0c\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u4f4e\u7248\u672c\u811a\u672c\u7684\u6d4f\u89c8\u5668 js\u4e2d\u672c\u8eab\u662f\u6ca1\u6709trim\u51fd\u6570\u7684 \/\/\u5220\u9664\u5de6\u53f3\u4e24\u7aef\u7684\u7a7a\u683c function trim(str){ return str.replace(\/(^s*)|(s*$)\/g, &#8220;&#8221;); } \/\/\u5220\u9664\u5de6\u8fb9\u7684\u7a7a\u683c function ltrim(str){ return str.replace(\/(^s*)\/g,&#8221;&#8221;); } \/\/\u5220\u9664\u53f3\u8fb9\u7684\u7a7a\u683c function rtrim(str){ return str.replace(\/(s*$)\/g,&#8221;&#8221;); } \u767b\u5f55\u540e\u590d\u5236 \u6216\u8005\u6269\u5c55String\u7c7b\u578b\u5c5e\u6027 String.prototype.trim = function() { return this.replace(\/(^s*)|(s*$)\/g, &#8220;&#8221;); } var str = document.getElementById(&#8220;test&#8221;).value; alert( str.trim() ); \u767b\u5f55\u540e\u590d\u5236 \u6216\u8005\u5e72\u8106\u653e\u5f03\u4f7f\u7528js,\u4f7f\u7528jQuer\u4e4b$.trim(str) var str = $(&#8220;#test&#8221;).val(); alert( $.trim(str) ); \u767b\u5f55\u540e\u590d\u5236 PS\uff1a\u793a\u4f8b\u4e2d\u7528\u5230\u4e86\u6b63\u5219\u64cd\u4f5c\uff0c\u8fd9\u91cc\u4e3a\u5927\u5bb6\u63d0\u4f9b2\u6b3e\u975e\u5e38\u65b9\u4fbf\u7684\u5de5\u5177\u4f9b\u5927\u5bb6\u53c2\u8003\u4f7f\u7528\uff1a \u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01 \u63a8\u8350\u9605\u8bfb\uff1a \u4ee5\u4e0a\u5c31\u662fjs\u7684\u81ea\u5b9a\u4e49trim\u51fd\u6570\u4f7f\u7528\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31390","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31390","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31390"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31390\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31390"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31390"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31390"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}