zoukankan      html  css  js  c++  java
  • jQuery 获取文件后缀的方法

    方法一、

           采用正则表达式:

    Js代码  收藏代码
    1. var file=$("input[name='file']").val();  
    2. var filename=file.replace(/.*(\/|\\)/, "");  
    3. var fileExt=(/[.]/.exec(filename)) ? /[^.]+$/.exec(filename.toLowerCase()) : '';  

     

      filename得到文件名

     

     

       fileExt得到后缀名

    方法二、

       

      

    Js代码  收藏代码
    1. var location=$("input[name='file']").val();  
    2.  var point = location.lastIndexOf(".");  
    3.   
    4.  var type = location.substr(point);  
    5.  if(type==".jpg"||type==".gif"||type==".JPG"||type==".GIF"){  
    6.            
    7.  }  

     

    获取文件名和后缀:

      1. 用split()来拆成数组 然后取下标0的。 
         var arr = sFileName.split('.');
         alert(arr[0]);

     

      2.使用一般后缀长度为4的特点:

        var name= sFileName.substr(0,sFileName.length-4)

     

      3.获取文件名的正则表达式:

        var sFileName=sFile.replace(/.*(\/|\\)/, "");

      

     去掉后缀:

       

    代码:

     

    Js代码  收藏代码
    1. <script >  
    2. $(document).ready(function(){  
    3.  var a=$("#image").attr("src")  
    4.  alert(a.replace(".png",""))  
    5.  })  
    6. </script>  
    7. <img alt="aaa" id="image" src="theImage.png" />   

     

  • 相关阅读:
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
    UVA10071 Back to High School Physics
    UVA10071 Back to High School Physics
    UVA10055 Hashmat the Brave Warrior
    UVA10055 Hashmat the Brave Warrior
    UVA458 The Decoder
    UVA458 The Decoder
    HDU2054 A == B ?
    HDU2054 A == B ?
    POJ3414 Pots
  • 原文地址:https://www.cnblogs.com/webu/p/2792036.html
Copyright © 2011-2022 走看看