javascript - 如何使用javascript查找文件?

标签 javascript jquery html iframe

我的应用正在查看一个文件夹,然后在下拉菜单中显示所有文件夹和其中的 html 文件,并在 iframe 中显示所有 html 文件。我有一个名为“highlighted.html”的文件,我不想在下拉菜单中显示它,但如果它在当前目录中,我确实想在 iframe 中自动显示它。

这是我的代码,用于显示文件夹中的内容:

  • 第一个函数创建一个下拉框来动态加载文件夹或文件(带有 html 扩展名)。

  • 在第二个功能中:如果单击现有的子文件夹,则打开该文件夹并在内部查找 html 文件以在 iframe 中打开它

    function rendSelects($currentSelectItem, strPath) {
        var currentSelectLevel = (null === $currentSelectItem ? -1 : parseInt($currentSelectItem.attr('data-selector-level'))),
        nextOneSelectorHtml =
            '<select ' +
            'class="dropdown selectpicker" ' +
            'name="dd" ' +
            'data-selector-level="' + (currentSelectLevel + 1) + '" ' +
            'data-path="' + strPath + '" ' +
            'onchange="onFsSelectChange(this)"' +
            '><option text selected> -- select an option -- </option>';
    
        $('div.selectors-container select.dropdown').each(function (i, el) {
            if (parseInt(el.getAttribute('data-selector-level')) > currentSelectLevel) {
                el.parentNode.removeChild(el);
                $(el).selectpicker('destroy');
            }
        });
    
        if (fsStructure[strPath].subfolders.length > 0) {
            for (var i = 0; i < fsStructure[strPath].subfolders.length; i++) {
                nextOneSelectorHtml +=
                    '<option ' +
                    'class="subfolder-option" ' +
                    'data-subfolder="' + fsStructure[strPath].subfolders[i] + '" >' + fsStructure[strPath].subfolders[i] +
                '</option>';
            }
        }
    
        if (fsStructure[strPath].subshtmls.length > 0) {
            for (var i = 0; i < fsStructure[strPath].subshtmls.length; i++) {
                nextOneSelectorHtml +=
                    '<option ' +
                    'class="html-page-option" ' +
                    'data-html-page-name="' + fsStructure[strPath].subshtmls[i] + '">' + fsStructure[strPath].subshtmls[i] +
                    '</option>';
            }
        }
    
        nextOneSelectorHtml += '</select>';
        $('div.selectors-container').append(nextOneSelectorHtml);
        $('div.selectors-container').trigger('dropdownadded.mh');
    }
    
    function onFsSelectChange(el) {
        var currentSelectorPath = el.getAttribute('data-path'),
            selectedOption = el.options[el.selectedIndex];
    
        if (selectedOption.classList.contains('subfolder-option')) {
            loadFolderStructure(currentSelectorPath + '/' + selectedOption.getAttribute('data-subfolder'), $(el))
        }
    
        if (selectedOption.classList.contains('html-page-option')) {
            playSwf(currentSelectorPath + '/' + selectedOption.getAttribute('data-html-page-name'));
        }    
    }
    

我在 http://tdhtestserver.herobo.com/ 提供了一个工作演示.

已解决

最佳答案

嗯。如果文件夹中确实存在highlighted.html,则不选择构成。让我们用 src=highlighted.html IIUC 显示一个 iFrame。我还好吗?

First function create a dropdown boxes where it load dynamically folders or files with html extension. Ok, so let's check if highlighted.html is here.

     function rendSelects($currentSelectItem, strPath) {
//here : (begin of change)             
if(strPath.indexOf("hightlighted")>=0) {
                       $("#myiFrame").attr('src', /path/to/highlighted)
                }

    // enfd of change. The continue as  :
    var currentSelectLevel = (null === $currentSelectItem ? -1 : parseInt($currentSelectItem.attr('data-selector-level'))),
        nextOneSelectorHtml =....

关于javascript - 如何使用javascript查找文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026170/

相关文章:

javascript - 无法使用本地存储设置值

javascript - 应用正则表达式进行文本突出显示

javascript - ajax 加载成功响应并加载旋转

Javascript/jQuery 如何将负载旋转器附加到语法荧光笔插件

javascript - 如何让我的页脚从顶部覆盖页面的 100% 高度和宽度?

html - 删除填充时 Div 会缩短

javascript - 如何在 typeScript 中将 $event returnValue 属性设置为 null

javascript - 将在 React 组件中解构的 props 默认值的影响

javascript - 如何使用 require() 使用另一个文件中的函数

html - 在同一个 css 中自定义不同的提交按钮