本文實例講述了自定義函數實現IE7與IE8不兼容js中trim函數的方法。分享給大家供大家參考。具體實現方法如下:
<html>
<head>
<title>test</title>
<script type="text/javascript">
String.prototype.trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}
function check(){
var str = document.getElementById("test").value;
alert(str.trim());
}
</script>
</head>
<body>
<center>
<input id="test" type="text" />
<input id="but" type="button" value="檢驗" onclick="check();"/>
</center>
</body>
</html>
這樣就解決了IE上js不支持trim的問題。
希望本文所述對大家的javascript程序設計有所幫助。