jQuery查找文件扩展名(从字符串)

我想知道是否有可能jQuery找到一个基于返回的字符串的文件扩展名?

文件名(字符串)将被传递给一个函数(openFile),我希望该函数根据已经传递的文件做不同的事情,它可以是图像文件或pdf文件

function openFile(file) { 

  //if .jpg/.gif/.png do something

  //if .zip/.rar do something else

  //if .pdf do something else

};

我一直在寻找的东西,会找到文件的扩展名,但我似乎找不到任何东西。

解决方法

这样的事情怎么样。

测试live示例:http://jsfiddle.net/6hBZU/1/

它假定字符串将总是以扩展名结束:

function openFile(file) {
    var extension = file.substr( (file.lastIndexOf('.') +1) );
    switch(extension) {
        case 'jpg':
        case 'png':
        case 'gif':
            alert('was jpg png gif');  // There's was a typo in the example where
        break;                         // the alert ended with pdf instead of gif.
        case 'zip':
        case 'rar':
            alert('was zip rar');
        break;
        case 'pdf':
            alert('was pdf');
        break;
        default:
            alert('who kNows');
    }
};

openFile("somestring.png");

编辑:我错误删除了openFile(“somestring.png”);的一部分字符串。更正。但在实例中,它。

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...