close

javascript 裡要削去字串空白可以使用trim,ltrim,rtrim

在IE上使用javascript的function trim()會發生錯誤,可以加上此段讓IE正常


if(typeof String.prototype.trim !== 'function') {

 String.prototype.trim = function() {

    return this.replace(/^\s+|\s+$/g,"");

 }

}

if(typeof String.prototype.ltrim !== 'function') {

 String.prototype.ltrim = function() {

    return this.replace(/^\s+/,"");

 }

}

if(typeof String.prototype.rtrim !== 'function') {

 String.prototype.rtrim = function() {

    return this.replace(/\s+$/,"");

 }

}
 
arrow
arrow
    全站熱搜

    Matt 發表在 痞客邦 留言(0) 人氣()