分享

jQuery.trim()用法

 若生安饶 2012-09-24

jQuery.trim( str ) Returns: String

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

  • version added: 1.0jQuery.trim( str )

    strThe string to trim.

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.

Examples:

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

<!DOCTYPE html> 
<html> 
<head> 
  <script src="http://code./jquery-latest.js"></script> 
</head> 
<body> 
   
        <pre id="original"></pre> 
        <pre id="trimmed"></pre> 
       
<script> 
  var str = "         lots of spaces before and after         "; 
  $("#original").html("Original String: '" + str + "'"); 
  $("#trimmed").html("$.trim()'ed: '" + $.trim(str) + "'"); 
</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?"

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约