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

jquery replace方法去空格

 更新時(shí)間:2017年05月08日 11:05:23   作者:yangzailu1990  
本篇文章主要介紹了jquery replace方法去空格的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧

話不多說,請(qǐng)看代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <title> new document </title>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <meta name="author" content="" />
 <meta name="keywords" content="" />
 <meta name="description" content="" />
 <link rel="stylesheet" type="text/css" href="" />
 <style type="text/css"></style>
 <script type="text/javascript" src="jquery-3.0.0.js"></script>
 </head>

 <body>
 <div style="width:300px;height:100px;border:1px solid #E5E5E5;margin:0 auto;margin-top:30px;">
 <form action="" method="post">
  <span>用戶名:</span><input type="text" name="username" class="username" value="" />
  <input type="submit" value="提交" class="tijiao">
 </form>
  <script type="text/javascript">
  $(document).ready(function(){
    $(".tijiao").click(function(){
       //var name = $.trim($(".username").val());
       var name= $('input[name=username]').val().replace(/(^\s*)|(\s*$)/g,"");
       if(name==""){
        alert("用戶名不能為空");
       }
     });
  });

  /*
    /(^\s*)|(\s*$)/g 包含以空格、回車符等字符開頭或者空格、回車符等字符結(jié)尾的字符串,可過濾出所有空格、回車符的字符
    /g意思就是:global可選標(biāo)志,帶這個(gè)標(biāo)志表示替換將針對(duì)行中每個(gè)匹配的串進(jìn)行,否則則只替換行中第一個(gè)匹配串。如:we.fdffddfwe.加上/g后,則2個(gè)we都會(huì)出來;
    \s 空白字符

    replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個(gè)與正則表達(dá)式匹配的子串。
    stringObject.replace(regexp/substr,replacement)
  */
  </script>
 <div>
 </body>
</html>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論