2015-06-24 6 views
5

La mia app sta esaminando una cartella e quindi mostra tutte le cartelle e il file html al suo interno in un menu a discesa e visualizza tutti i file html all'interno di un iframe. Ho un file chiamato "evidenziato.html" che non voglio mostrare nel menu a tendina, ma se è nella directory corrente voglio mostrarlo automaticamente in un iframe.Come trovare un file con javascript?

Questo è il mio codice per mostrare ciò che è nella cartella:

  • Prima funzione di creare una casella a discesa di carico dinamico cartelle o file (con estensione html).

  • In seconda funzione: se click su una sottocartella esistente, quindi aprire la cartella e guardare all'interno per file html (s) per aprirlo in un 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')); 
        }  
    } 
    

Ho fornito un lavoro demo allo http://tdhtestserver.herobo.com/.

risolto

risposta

0

rispondo a mia domanda, perché alcuni di voi non hanno capito il mio problema o non sanno come farlo. Quindi ho scoperto che era così facile e tutto ciò che ho fatto era solo una riga di codice.

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

Questa linea è stato cambiato tutto, in cui high è un nuovo 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

Bene. Se evidenziato.html esiste nella cartella, nessuna selezione selezionata. Mostriamo un iFrame con src = evidenziato.html IIUC. Sto bene?

Prima funzione creare un menu a discesa in cui caricare dinamicamente cartelle o file con estensione html. Ok, quindi controlliamo se evidenziato.html è qui.

 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

Non si può dire '$ ("# myiFrame").attr ('src',/percorso/a/evidenziato) 'perché il percorso è creato dinamicamente come' fsStructure [strPath] .subshtmls [i] ' –

+0

@MarkusHayner: no prob '. Dì $ ("# myiFrame"). Attr ('src',/percorso/a/evidenziato) in seguito. IN REALTÀ, si tratta di scegliere tra: 1. $ (myframeid) .attr (src ...) AND 2. Si prega di vedere la mia un'altra risposta – 3pic

0

In realtà, la questione è di scegliere tra:. 1. $ (myframeid) .attr (src ...) e 2. $ ('div.selectors-container') append (nextOneSelectorHtml) ; /// devi "renderizzare" 1 o 2, a seconda della ricerca evidenziata o meno.

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 

Beh, se visualizzare solo cambiato: - alla funzione fin dall'inizio:

//1of3 
    var is_highlighted_here = false; 
    var highlighted_path=""; 
  • durante l'analisi del struct cartella:

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

  • E infine, durante il rendering:

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

+0

Questo sta facendo esattamente la stessa cosa di prima di nessun cambiamento. Basta chiamare gli stessi file. Devo chiamare un file specificato. –

+0

Ok, fornire un collegamento con l'origine completa @MarkusHayner. Ok per aiutarti, non molto tempo però. Fornire fonti. NOTA: il tuo iFrame potrebbe aver bisogno di un aggiornamento. – 3pic