2016-06-19 27 views
2

我想從動態更改的數組中獲取文件路徑並將其存儲在值c中。當我點擊激活它的按鈕時,它會返回Web地址中文件在數組exp中的編號。我想要www.anon-curb.com/swfs/exampleflash.swf,但它給了我www.anon-curb.com/swfs/464; 464是它在陣列中的順序。如何從JavaScript中的數組中獲取文本並將其附加到texarea?

HTML

<div class="share_con"> 
       <button id="share">Get a link!</button> 
       <textarea id="link"></textarea> 
      </div> 

JAVASCRIPT

var c = sessionStorage.getItem('order'); 
    sessionStorage.setItem('order', c); 
    var flashcon, test, temp; 

    share = document.getElementById('share'); 
    link = document.getElementById('link'); 

    function init() { 

     share.onclick = function() { 
      console.log('Event: share button clicked'); 
      shareshare(); 
     } 

    } 

    function shareshare() { 
     console.log('Called: shareshare()'); 
     link.innerHTML = 'www.anon-curb.com/' + c; 
    } 

    // What displays the actual flash and title for the flash 
    function displayFiles() { 
     console.log('Called: displayFiles()'); 
     test = paths[c].substring(paths[c].lastIndexOf('.') + 1, paths[c].length); 
     document.getElementById('title').innerHTML = displaytext[c]; 

     flashcon.innerHTML = 
      '<object id="flashcontent" data="' + paths[c] + '">' + 
      '<param name="movie" type="application/x-shockwave-flash">' + 
      '</object>'; 
    } 
    window.addEventListener ? 
     window.addEventListener('load', init, false) : 
     window.attachEvent('onload', init); 
}); 

所有變量的創建和正確調用我唯一的問題是我不知道如何獲得包含在數組中,而不是訂單的實際文本它進來了。

+0

你的陣列是可引用的?使用括號符號'arr [c]' –

+0

我給了我更多的JavaScript代碼,我認爲這會有所幫助 – cosmichero2025

+0

你可以給我們一個'sessionStorage'調試嗎? – MayorMonty

回答

0

所以我做了一些擺弄,我做了「雖然我很笨!爲什麼我不使用c的數字來調用數組中的文本?」這就是我做的!

function shareshare() { 
     var l = paths[c]; 
     console.log('Called: shareshare()'); 
     link.innerHTML = 'www.anon-curb.com/' + l; 
    } 

順便說我有NOOOO知道爲什麼我沒有立刻覺得blahhhh

+1

是的,這是我在評論發佈前2分鐘的建議。 – trincot

+0

Yaaa我知道,但我沒有更新頁面BC我滾動下來,當我choding它,所以我沒有看到, – cosmichero2025

+1

沒有問題。最重要的是你有一個解決方案:) – trincot

1

認爲這是我一直在尋找通過您的代碼,我發現了一些問題:

  1. 文字區域使用innerText,而不是innerHTML

    這是一個簡單的修復,只需切換shareshare

    function shareshare() { 
        console.log('Called: shareshare()'); 
        link.innerText = 'www.anon-curb.com/' + c; 
    } 
    

還拿你的解決方案的提示:

function shareshare() { 
    var l = paths[c]; 
    console.log('Called: shareshare()'); 
    link.innerHTML = 'www.anon-curb.com/' + l; 
} 

希望我能有點用(即使你已經理解了它)

+0

我很感謝你的回答,但innerHTML工作得很好:D – cosmichero2025

+0

呵呵。在我的Chrome版本(51)中,它不起作用,並且從我從規範中讀取的內容中,使用了「innerText」。 TIL,猜! – MayorMonty

相關問題