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

jquery $.trim()去除字符串空格的實(shí)現(xiàn)方法【附圖例】

 更新時(shí)間:2016年03月30日 16:27:39   投稿:jingxian  
下面小編就為大家?guī)硪黄猨query $.trim()去除字符串空格的實(shí)現(xiàn)方法【附圖例】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

jquery $.trim()去除字符串空格的實(shí)現(xiàn)方法【附圖例】

語法
jQuery.trim()函數(shù)用于去除字符串兩端的空白字符。

作用
該函數(shù)可以去除字符串開始和末尾兩端的空白字符(直到遇到第一個(gè)非空白字符串為止)。它會(huì)清除包括換行符、空格、制表符等常見的空白字符。

參數(shù)
如果參數(shù)str不是字符串類型,該函數(shù)將自動(dòng)將其轉(zhuǎn)為字符串(一般調(diào)用其toString()方法)。如果參數(shù)str為null或undefined,則返回空字符串("")。

返回值
jQuery.trim()函數(shù)的返回值為String類型,返回去除兩端空白字符串后的字符串。

示例&說明

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
 <head>
  <base href="<%=basePath%>">
  
  <title>My JSP 'index.jsp' starting page</title>
  <meta http-equiv="pragma" content="no-cache">
  <meta http-equiv="cache-control" content="no-cache">
  <meta http-equiv="expires" content="0">  
  <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
  <meta http-equiv="description" content="This is my page">
  <!--
  <link rel="stylesheet" type="text/css" href="styles.css">
  -->
 </head>
 
 <body>
 
  <input type="text" name="" id="results" value=""/><br>
  
  <button id="showBtn">showBtn</button>
  <button id="showBtn1">showBtn1</button>
  
  <script src="http://code.jquery.com/jquery-1.5.js"></script>
  <script type="text/javascript">
  
  $("#showBtn").click( function(){
      var content = $('#content').val();
     if($.trim(content) == ''){
      alert('空');
     }
  } );
  
  $("#showBtn1").click( function(){
      var content = $('#content').val();
     if(content.trim() == ''){
      alert('空');
     }
  } );
  
  
   
  </script>
  
 </body>
</html>

 

 不能理所當(dāng)然的想跟java一樣。用一個(gè)字符串點(diǎn)方法。

錯(cuò)誤寫法:

 $("#showBtn1").click( function(){
      var content = $('#content').val();
     if(content.trim() == ''){
      alert('空');
     }
  } );

說明: 上面的寫法在firefox下不會(huì)報(bào)錯(cuò),在ie和谷歌下確會(huì)報(bào)錯(cuò)。

 正確寫法:

$("#showBtn").click( function(){
      var content = $('#content').val();
     if($.trim(content) == ''){
      alert('空');
     }
  } );

以上這篇jquery $.trim()去除字符串空格的實(shí)現(xiàn)方法【附圖例】就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論