- 相關(guān)推薦
有關(guān)javascript在IE下trim函數無(wú)法使用的解決方法
首先,javascript的trim函數在firefox下面使用沒(méi)有問(wèn)題:
var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim();
在火狐下這樣用沒(méi)有問(wèn)題, 但是在IE下就報錯!
對此,我們可以修改一下:
String.prototype.trim=function(){return this.replace(/(^s*)|(s*$)/g,"");}
在頭上加上這一句,上面的就可以在IE和FF下都可以運行了:
String.prototype.trim=function(){return this.replace(/(^s*)|(s*$)/g,"");} var test1 = " aa "; test1 = test1.toString(); test1 = test1.trim();
JQuery提供的方法:
Show Trim Example
$("button").click(function () { var str = " lots of spaces before and after "; alert(" + str + "); str = jQuery.trim(str); alert(" + str + - no longer"); });
【javascript在IE下trim函數無(wú)法使用的解決方法】相關(guān)文章:
電腦開(kāi)機后鼠標無(wú)法使用的解決方法01-12
IE與Firefox在JavaScript上的不同寫(xiě)法小結07-22
Javascript函數的定義和用法分析08-15
JavaScript中push(),join() 函數實(shí)例詳解09-05
最常用的20個(gè)javascript方法函數09-10
CAD無(wú)法填充解決方法11-25