获取给定目录jQuery中的文件名

Jor*_*123 3 directory jquery directory-listing

我想得到一个文件名列表,稍后将存储到一个数组中.以下代码只是以与浏览器中显示Apache列表相同的方式显示目录列表,例如1234.txt 31-Aug-2016 13:17 35K如何更改它,所以我只得到文件名?

<script type="text/javascript">
$(document).ready(function () {
  $.get("dat/", function(data) {
    $("#files").append(data);
  });
});
</script>
<body>
<div id='files'></div>
</body>
Run Code Online (Sandbox Code Playgroud)

Rah*_*tel 7

请尝试使用以下代码.openFile函数可用于检查它是文件还是文件夹.请根据您的使用情况添加更多功能扩展.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
    var fileNames = new Array();
    $.ajax({
      url: "/test/",
      success: function(data){
         $(data).find("td > a").each(function(){
            if(openFile($(this).attr("href"))){
                fileNames.push($(this).attr("href"));
            }           
         });
      }
    }); 
    console.log(fileNames);
    function openFile(file) {
        var extension = file.substr( (file.lastIndexOf('.') +1) );
        switch(extension) {
            case 'jpg':
            case 'png':
            case 'gif':   // the alert ended with pdf instead of gif.
            case 'zip':
            case 'rar':
            case 'pdf':
            case 'php':
            case 'doc':
            case 'docx':
            case 'xls':
            case 'xlsx':
                return true;
                break;
            default:
                return false;
        }
    };
</script>
Run Code Online (Sandbox Code Playgroud)

  • 做得很好。不需要那个break,return就可以了,我们需要在实现逻辑之前解析html。 (2认同)