返回值:StringjQuery.trim(str)

Remove the whitespace from the beginning and end of a string.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

示例:

Remove the two white spaces at the start and at the end of the string.

<!DOCTYPE html>
<html>
<head>
<script src="jquery.min.js"></script>
</head>
<body>

<button>Show Trim Example</button>

<script>



$("button").click(function () {
var str = "     lots of spaces before and after     ";
alert("'" + str + "'");

str = jQuery.trim(str);
alert("'" + str + "' - no longer");
});



</script>
</body>
</html>
演示:

示例:

Remove the two white spaces at the start and at the end of the string.

jQuery 代码:
$.trim("  hello, how are you?  ");
结果:
"hello, how are you?"