JQ:html
$.trim() 函數用於去除字符串兩端的空白字符。函數
注意:$.trim()函數會移除字符串開始和末尾處的全部換行符,空格(包括連續的空格)和製表符。若是這些空白字符在字符串中間時,它們將被保留,不會被移除。spa
$(function () { var str = " lots of spaces before and after "; $( "#original" ).html( "Original String: '" + str + "'" ); $( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" ); }) 輸出結果 Original String: ' lots of spaces before and after ' $.trim()'ed: 'lots of spaces before and after'
原生JScode
trim() 方法用於刪除字符串的頭尾空格。htm
trim() 方法不會改變原始字符串。blog
語法:string.trim()字符串
var str = " Runoob "; alert(str.trim()); 輸出結果 Runoob