2015-06-24 12 views
5

Moja aplikacja szuka w folderze, a następnie wyświetla wszystkie foldery i pliki HTML w menu rozwijanym oraz wyświetla wszystkie pliki HTML w elemencie iframe. Mam plik o nazwie "highlighted.html", którego nie chcę wyświetlać w menu rozwijanym, ale jeśli znajduje się w bieżącym katalogu, chcę go automatycznie wyświetlić w elemencie iframe.Jak znaleźć plik z javascript?

To jest mój kod, aby zobaczyć, co znajduje się w folderze:

  • Pierwsza funkcja tworzenia ładowanie listy rozwijanej dynamicznie folderów lub plików (z rozszerzeniem html).

  • W drugiej funkcji: jeśli kliknięcie na istniejącym podfolderu, a następnie otwórz ten folder i zajrzeć do pliku (ów) html, aby otworzyć go w 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')); 
        }  
    } 
    

I dostarczyły pracę wersja demo pod adresem http://tdhtestserver.herobo.com/.

SOLVED

Odpowiedz

0

mi odpowiedzieć na moje pytanie, ponieważ niektórzy z was nie zrozumiałeś mojego problemu lub nie wiedzą jak to zrobić. Stwierdziłem więc, że to takie proste i wszystko co zrobiłem to tylko jedna linia kodu.

high(currentSelectorPath + '/'+selectedOption.getAttribute('data-subfolder')+'/highlighted.html'); 

Linia ta została zmieniona, gdzie wszystko jest nowym high iframe

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)) 
    high(currentSelectorPath + '/'+selectedOption.getAttribute('data-subfolder')+'/highlighted.html'); 
    } 
    if (selectedOption.classList.contains('html-page-option')) { 
    playSwf(currentSelectorPath + '/' + selectedOption.getAttribute('data-html-page-name')); 
    } 

} 
0

Dobrze. Jeśli plik highlighted.html istnieje w folderze, nie ma konstytucji wyboru. Wyświetlmy element iFrame z src = highlighted.html IIUC. Czy jestem OK?

Pierwsza funkcja tworzenia rozwijanych skrzynek, w których dynamicznie ładuje się foldery lub pliki z rozszerzeniem html. OK, sprawdźmy więc czy zaznaczone.html jest tutaj.

 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 =.... 
+0

Nie można powiedzieć, '$ ("# myiFrame").attr ('src',/path/to/highlighted) 'ponieważ ścieżka jest tworzona dynamicznie jako' fsStructure [strPath] .subshtmls [i] ' –

+0

@MarkusHayner: no prob '. Powiedz $ ("# myiFrame"). Attr ('src',/path/to/highlighted) później. FAKTYCZNIE, kwestią jest wybór: 1. $ (myframeid) .attr (src ...) ORAZ 2. Proszę zobaczyć moją inną odpowiedź – 3pic

0

Faktycznie, sprawa jest wybór pomiędzy:. 1. $ (myframeid) .attr (src ...) oraz 2. $ ('div.selectors-kontener') dołączy (nextOneSelectorHtml) ; /// musisz "wyrenderować" 1 lub 2, w zależności od znalezionego podświetlenia lub nie.

function rendSelects($currentSelectItem, strPath) { 
//1of3 // let's add a boolean 
var is_highlighted_here = false; 
var highlighted_path=""; 
//done. 

    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++) { 

// 2of3 // oh !! look at here : 
if(fsStructure[strPath].subshtmls[i].indexOf("highlighted")>=0) 
{ 
s_highlighted_here=true; 
highlighted_path = fsStructure[strPath].subshtmls[i]; 
} 
//done. scroll to bottom. 


      nextOneSelectorHtml += 
       '<option ' + 
       'class="html-page-option" ' + 
       'data-html-page-name="' + fsStructure[strPath].subshtmls[i] + '">' + fsStructure[strPath].subshtmls[i] + 
       '</option>'; 
     } 
    } 

    nextOneSelectorHtml += '</select>'; 
// 3of3 // here finally 
    if(is_highlighted_here) { 
         $("#myiFrame").attr('src', highlighted_path); 
    } 
    else { 
    $('div.selectors-container').append(nextOneSelectorHtml); 
    $('div.selectors-container').trigger('dropdownadded.mh'); 
    } 

}//function end 

Dobrze, jeśli mogę wyświetlać zmienić tylko: - na samym początku funkcji:

//1of3 
    var is_highlighted_here = false; 
    var highlighted_path=""; 
  • podczas analizowania struct folderu:

    // 2of3 // oh !! look at here : if(fsStructure[strPath].subshtmls[i].indexOf("highlighted")>=0) { s_highlighted_here=true; highlighted_path = fsStructure[strPath].subshtmls[i]; }

  • I wreszcie podczas renderowania:

    // 3of3 if(is_highlighted_here) { $("#myiFrame").attr('src', highlighted_path); } else { $('div.selectors-container').append(nextOneSelectorHtml); $('div.selectors-container').trigger('dropdownadded.mh'); }

+0

To robi dokładnie to samo, co przed zmianą. Po prostu dzwonisz do tych samych plików. Muszę zadzwonić pod określony plik. –

+0

OK podaj link do pełnego źródła @MarkusHayner. Ok, aby ci pomóc, nie za dużo czasu. Podaj źródła. UWAGA: Twoja iFrame może wymagać odświeżenia. – 3pic