jQuery.trim()

jQuery.trim( str ) 官网原E文: String

描述: 去掉字符串起始和结尾的空格。

  • version added: 1.0jQuery.trim( str )

    str修剪的字符串。

$.trim()函数删除提供字符串的所有换行符,空格(包括非中断空格),开始和结束tab。如果这些空白字符在字符串中间发生时,它们将被保留。

Examples:

Example: 删除在开始和在字符串的末尾两个空格。

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.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>

Demo:

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

$.trim("  hello, how are you?  ");

Result:

"hello, how are you?"
jQuery 1.6 API 中文版Clove整理、修订