javascript的trim功能

來源:互聯網
上載者:User

 今天在寫js的時候碰到一個錯誤,js代碼如下:

 

//驗證日期格式的正則。如  2007-09-12
function  isDate(str)...{   
  var days=new Array(31,28,31,30,31,30,31,31,30,31,30,31);     
  var a=str.match(/^(d...{4})(-)(d...{1,2})(-)(d...{1,2})$/);   
  if(a ==null) return false;   
  days[1]=((0==a[1]%4)&&(0!=(a[1]%100)))||(0 ==a[1] %400)?29:28;     
  if(a[1]<=1900||a[3]==0||a[3]>=13||a[5]==0||a[5]>days[a[3]-1]) 
      return false;   
  return   true;   
}

function editTime()...{
    var startDate=document.getElementById("meetStartDate"); 
    var endDate=document.getElementById("meetEndDate"); 
    if(!isDate(startDate.value.trim()))
        ...{
            alert('<bean:message key="label.editMeetTime.startTime"/>'+' '+'<bean:message key="msg.meetBoard.StartDateIsError"/>');
            startDate.focus();
            return;
        }
    if(!isDate(endDate.value.trim()))
        ...{
            alert('<bean:message key="label.editMeetTime.endTime"/>'+' '+'<bean:message key="msg.meetBoard.StartDateIsError"/>');
            endDate.focus();
            return;
        }

    editMeetTimeForm.action='saveMeetTime.do';
    editMeetTimeForm.submit();
    window.opener.location='meetBoard.do?meetingId='+'<bean:write name="editMeetTimeForm" property="meetDetailId"/>'; 
    window.close();
}

 

黃色標誌處有錯,說不支援這個屬性。把trim()去掉後,就沒錯了。記得以前這樣使過,沒有報錯。查了一下以前可以支援trim的頁面,發現如果在頁面上匯入一個有關ajax的js就ok了,如下:

<script type="text/javascript" src="js/ajaxtags/ajaxtags-1.2-beta2.js"></script>

在這個js裡搜了一下trim,發現有這樣的一個方法:

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

於是把上面匯入的js去掉,單把這個方法加進來,也ok。至於為什麼,還不清楚,改日找時間再研究一下。

最近感覺跟js嗑上了……

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.