請選擇你要去除的空白字串按鈕 |
輸入要去除空白字串: 輸出去除的結果字串:
|
下列是 JavaScript 程式的內容: |
<HEAD> <script type="text/javascript" language="JavaScript"> //去除前後(左右)空白 String.prototype.trim = function() { alert('已去除左右邊空白'); return this.replace(/(^[\s]*)|([\s]*$)/g, ""); } //去左空白 String.prototype.lTrim = function() { alert('已去除左邊空白'); return this.replace(/(^[\s]*)/g, ""); } //去除右空白 String.prototype.rTrim = function() { alert('已去除右邊空白'); return this.replace(/([\s]*$)/g, ""); } function callTrim() { document.getElementById("ans").value = document.getElementById("content").value.trim(); } function callLTrim() { document.getElementById("ans").value = document.getElementById("content").value.lTrim(); } function callRTrim() { document.getElementById("ans").value = document.getElementById("content").value.rTrim(); } </script> </HEAD> <BODY> 輸入要去除空白字串:<input type="text" id="content" value=" 輸 入 要 去 空 白 字 串 "><br> 輸出去除的結果字串:<input type="text" id="ans"> <p> <button onclick="callTrim();">去除左右空白</button> <button onclick="callLTrim();">去除左空白</button> <button onclick="callRTrim();">去除右空白</button> </BODY> |
本報導資料來源:本資料取自聰明的生活-教學文件與筆記
![]() |
![]() ![]() ![]() |
請按瀏覽器的檢視原始碼查看