2016-04-21 68 views
0

我正在做一個flash站點來改變它下載鏈接每次在頁面上選擇一個按鈕到corespronding flash文件下載。奇怪的部分是我完美的完成了這項工作,但在項目工作一週後,它停止了,我不知道爲什麼輸入會很好。爲什麼我的動態變化鏈接不起作用? (Javascript)

 $(document).ready(function() { 
      var links = [ 
     'swfs/#1%20(Special%20Japanese%20Extended%20Dance%20Mix).swf', 
     'swfs/$D6.swf', 
     'swfs/(MAD)%20Huh.swf' 
     ]; 

     var displaytext = [ 
     '#1 (Special Japanese Extended Dance Mix)', 
     '$D6', 
     '(MAD) Huh' 
     ]; 

     var c = 0; 
      var flashmovie, test, temp; 

      function init() { 
       flashmovie = document.getElementById('flashmovie'); 
       document.getElementById('back').onclick = function() { 
        if (c == 0) { 
         c = links.length; 
        } 
        c-- 
        displayFiles(); 
        download(); 
       } 

       document.getElementById('next').onclick = function() { 
        if (c == links.length - 1) { 
         c = -1; 
        } 
        c++; 
        displayFiles(); 
        download(); 
       } 

       document.getElementById('rand').onclick = function() { 
        temp = c; 
        while (c == temp) { 
         c = Math.floor(Math.random() * links.length); 
        } 
        displayFiles(); 
        download(); 
       } 

// Scripts for the left and right arrow key functionality 
     document.addEventListener('keydown', function (e) { 
      if (e.which == 37) { 
       $("#back").click(); 
      } 
     }); 

     document.addEventListener('keydown', function (e) { 
      if (e.which === 39) { 
       $("#next").click(); 
      } 
     }); 

      } 

      function displayFiles() { 

       test = links[c].substring(links[c].lastIndexOf('.') + 1, links[c].length); 
       document.getElementById('title').innerHTML = displaytext[c]; 

       flashmovie.innerHTML = 
        '<object type="application/x-shockwave-flash" data="' + links[c] + '">' + 
        '<param name="movie" value="' + links[c] + '">' + 
        '<\/object>'; 
      } 

      function download() { 
       document.getElementById('downLink').setAttribute('href', links[c]); 
       document.getElementById('downLink').setAttribute('download', displaytext[c]); 
      } 

      window.addEventListener ? 
       window.addEventListener('load', init, false) : 
       window.attachEvent('onload', init); 
     }); 

HTML

<body> 

    <div class="titleText"> 
      <h1>Anon Curb</h1> 
    </div> 
    <div id="flashmovie" class="flashMovieSamp"> 
     <object type="application/x-shockwave-flash" data="swfs/welcomeflash.swf">'+ 
      <param name="movie" value="http://www.anon-curb.com/swfs/welcomeflash.swf"> 
     </object> 
    </div> 
    <!-- end #container --> 
    <div id="buttonCon"> 

     <div id="buttons"> 
      <button id="next">next</button> 

      <button id="rand">Random</button> 

      <button id="back">Back</button> 
     </div> 

    </div> 

    <div id="titleCon"> 
     <a href="swfs/welcomeflash.swf" class="downLink" download="welcomeflash"> 
      <div id="title">Hit random button</div> 
     </a> 
    </div> 

     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script> 

    <script src="js/flashcollection.js"></script> 
</body> 
+0

你嘗試將其關閉並重新開啓? – Malk

+1

我試過這樣做,但然後火花開始飛出機器的嘴巴不知道如何解決它雖然我會打電話給坦率,雖然他可能有一個想法 – cosmichero2025

+0

哈哈! :)雖然,你應該指定它不工作的方式 – Malk

回答

1

主要的問題是功能下載(),你試圖通過ID「下行」來獲得元素,但沒有分配中的HTML標籤的ID,所以我現在將id ='downLink'添加到標籤。下行鏈路現在工作正常。

只是有點建議:因爲您已經有了jQuery,所以在處理DOM時可以使用jQuery選擇器。這樣更方便也有助於保持代碼的一致性。

$(document).ready(function() { 
 
    var links = [ 
 
     'swfs/#1%20(Special%20Japanese%20Extended%20Dance%20Mix).swf', 
 
     'swfs/$D6.swf', 
 
     'swfs/(MAD)%20Huh.swf' 
 
    ]; 
 

 
    var displaytext = [ 
 
     '#1 (Special Japanese Extended Dance Mix)', 
 
     '$D6', 
 
     '(MAD) Huh' 
 
    ]; 
 

 
    var c = 0; 
 
    var flashmovie, test, temp; 
 

 
    function init() { 
 
     flashmovie = document.getElementById('flashmovie'); 
 
     document.getElementById('back').onclick = function() { 
 
      if (c == 0) { 
 
       c = links.length; 
 
      } 
 
      c-- 
 
      displayFiles(); 
 
      download(); 
 
     } 
 

 
     document.getElementById('next').onclick = function() { 
 
      if (c == links.length - 1) { 
 
       c = -1; 
 
      } 
 
      c++; 
 
      displayFiles(); 
 
      download(); 
 
     } 
 

 
     document.getElementById('rand').onclick = function() { 
 
      temp = c; 
 
      while (c == temp) { 
 
       c = Math.floor(Math.random() * links.length); 
 
      } 
 
      displayFiles(); 
 
      download(); 
 
     } 
 

 
// Scripts for the left and right arrow key functionality 
 
     document.addEventListener('keydown', function (e) { 
 
      if (e.which == 37) { 
 
       $("#back").click(); 
 
      } 
 
     }); 
 

 
     document.addEventListener('keydown', function (e) { 
 
      if (e.which === 39) { 
 
       $("#next").click(); 
 
      } 
 
     }); 
 

 
    } 
 

 
    function displayFiles() { 
 

 
     test = links[c].substring(links[c].lastIndexOf('.') + 1, links[c].length); 
 
     document.getElementById('title').innerHTML = displaytext[c]; 
 

 
     flashmovie.innerHTML = 
 
      '<object type="application/x-shockwave-flash" data="' + links[c] + '">' + 
 
      '<param name="movie" value="' + links[c] + '">' + 
 
      '<\/object>'; 
 
    } 
 

 
    function download() { 
 
     document.getElementById('downLink').setAttribute('href', links[c]); 
 
     document.getElementById('downLink').setAttribute('download', displaytext[c]); 
 
    } 
 

 
    window.addEventListener ? 
 
     window.addEventListener('load', init, false) : 
 
     window.attachEvent('onload', init); 
 
});
<html> 
 
    <head> 
 
    <title>Title</title> 
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script> 
 
    <script src="js/flashcollection.js"></script> 
 
    </head> 
 
<body> 
 
<div class="titleText"> 
 
    <h1>Anon Curb</h1> 
 
</div> 
 
<div id="flashmovie" class="flashMovieSamp"> 
 
    <object type="application/x-shockwave-flash" data="swfs/welcomeflash.swf">'+ 
 
     <param name="movie" value="http://www.anon-curb.com/swfs/welcomeflash.swf"> 
 
    </object> 
 
</div> 
 
<!-- end #container --> 
 
<div id="buttonCon"> 
 

 
    <div id="buttons"> 
 
     <button id="next">next</button> 
 

 
     <button id="rand">Random</button> 
 

 
     <button id="back">Back</button> 
 
    </div> 
 

 
</div> 
 

 
<div id="titleCon"> 
 
    <a href="swfs/welcomeflash.swf" class="downLink" download="welcomeflash" id="downLink"> 
 
     <div id="title">Hit random button</div> 
 
    </a> 
 
</div> 
 
</body> 
 
</html>

+0

Omggggg多數民衆贊成合法的第十次在過去一年做了類似的項目。我真的非常需要開始關注這個問題,不過謝謝您先生提醒我的愚蠢:D – cosmichero2025

+0

@ cosmichero2025,在Chrome/Firefox中使用「檢查」和「控制檯」將有助於調試。 :) – Kelvin