欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript在IE下trim函數(shù)無法使用的解決方法

 更新時間:2014年09月12日 11:10:20   投稿:shichen2014  
這篇文章主要介紹了javascript在IE下trim函數(shù)無法使用的解決方法,分別敘述了javascript以及jQuery下的解決方案,對于WEB前端javascript設(shè)計人員進(jìn)行瀏覽器兼容性調(diào)試有不錯的借鑒價值,需要的朋友可以參考下

本文實例分析了javascript在IE下trim函數(shù)無法使用的解決方法,對于web前段設(shè)計有一定的借鑒價值。具體分析如下:

首先,javascript的trim函數(shù)在firefox下面使用沒有問題:

<script language="javascript"> 
 var test1 = "  aa  "; 
 test1 = test1.toString(); 
 test1 = test1.trim(); 
</script>

在火狐下這樣用沒有問題, 但是在IE下就報錯!

對此,我們可以修改一下:

String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");} 

在頭上加上這一句,上面的就可以在IE和FF下都可以運(yùn)行了:

<script language="javascript"> 
 String.prototype.trim=function(){return this.replace(/(^\s*)|(\s*$)/g,"");} 
 var test1 = "  aa  "; 
 test1 = test1.toString(); 
 test1 = test1.trim(); 
</script> 

JQuery提供的方法:

<!DOCTYPE html>  
<html>  
<head>  
 <script src="http://code.jquery.com/jquery-latest.js"></script>  
</head>  
<body>  
 <button>Show Trim Example</button>  
<script>  
$("button").click(function () {  
var str = "   lots of spaces before and after   ";  
alert("'" + str + "'");  
str = jQuery.trim(str);  
alert("'" + str + "' - no longer");  
});  
</script>  
</body>  
</html>

相信本文所述對大家利用javascript進(jìn)行WEB前端瀏覽器的兼容性設(shè)計有不錯的借鑒價值。

相關(guān)文章

最新評論