標籤:style blog io ar color sp java strong on
調用名為$.trim的工具函數,能刪除字串中左右兩邊的空格符,但該函數不能刪除字串中間的空格,調用格式為:
$.trim (str);
參數str表示需要刪除左右兩邊空格符的字串。
<body> <div id="divtest"> <div class="title"> <span class="fl">字串操作函數</span> <span class="fr"> <input id="btnShow" name="btnShow" type="button" value="計算" /> </span> </div> <div class="content"> <input id="txtName" name="txtName" type="text" /> <div class="tip"></div> </div> </div> <script type="text/javascript"> $(function () { $("#btnShow").bind("click", function () { $(".tip").html(""); var strTmp = "內容:"; var strOld = $("#txtName").val(); var strNew =$.trim(strOld); strTmp += strOld; strTmp += "<br/><br>除掉空格符前的長度:" strTmp += strOld.length; strTmp += "<br/><br>除掉空格符後的長度:" strTmp += strNew.length; $(".tip").show().append(strTmp); }); }); </script></body>
內容: dfddf
除掉空格符前的長度:9
除掉空格符後的長度:5
jQuery 工具類函數-字串操作函數