写一个function,清除字符串前后的空格 使用自带接口trim(),考虑兼容性: if (!String.prototype.trim) { String.prototype.trim = function() { return this.replace(/^\s+/, "").replace(/\s+$/,""); } } // test the function var str = " \t\n test string ".trim(); alert(str == "test string"); // alerts "true" 代码预览