2014-10-20 79 views
-1

index.html的Pastebin:http://pastebin.com/kdKFqTxe 只需复制并粘贴并运行它(此工作但有一些损坏的图像链接& no css)。 关于pastebin,只需点击一个节点,然后点击视频下方第一个破碎的图像。应该发生什么是一个对话框应该出现链接到文章(从tubeArray)。所有相关的代码都粘贴在下面。根据数组中的数组更改div的内容

我试图动态更改div的内容,当我点击图像。图像具有相应的id(内部数组中的第一个索引),第一个内部数组中有另一个数组(索引3)。我想用点击图像时使用JQuery的链接来填充div(id =“articleLinks”)。

的JavaScript & JQuery的:

的管阵列。 *注意:tubeArray中每个元素的第一个索引是ID &新闻文章没有链接到任何特定的东西。只关心tubeArray [0] & tubeArray [4]

var tubeArray = [ 
      ['UQ', -27.495134, 153.013502, "http://www.youtube.com/embed/uZ2SWWDt8Wg", 
       [ 
       ["example.com", "Brisbane students protest university fee hikes"], 
       ["example.com", "Angry protests over UQ student union election"], 
       ] 
      ], 
      ['New York', 40.715520, -74.002036, "http://www.youtube.com/embed/JG0wmXyi-Mw", 
       [ 
       ["example.com" , "NY taxpayers’ risky Wall Street bet: Why the comptroller race matters"] 
       ] 
      ], 
      ['To The Skies', 47.09399, 15.40548, "http://www.youtube.com/embed/tfEjTgUmeWw", 
       [ 
       ["example.com","Battle for Kobane intensifies as Islamic State uses car bombs, Syrian fighters execute captives"], 
       ["example.com","Jihadists take heavy losses in battle for Syria's Kobane"] 
       ] 
      ], 
      ['Fallujah', 33.101509, 44.047308, "http://www.youtube.com/embed/V2EOMzZsTrE", 
       [ 
       ["example.com","Video captures family cat saving California boy from dog attack"], 
       ["example.com","Fines of £20,000 for dogs that chase the postman"] 
       ] 
      ] 
     ]; 

一种用于环路经过在tubeArray每个元素然后分配ID到第一索引。另外一个图像,调用功能myFunctionId它采取参数this.id

for (i = 0; i < tubeArray.length; i++) { 
    var id = tubeArray[i][0]; 

    //other code 

    '<img src="img.png" onclick="myFunctionId(this.id);" id="' + id + '">' + 

    //other code 
} 

function myFunctionId (id) { 
     journal = id; 
     alert(journal) //just a test 

     //I want to search through tubeArray with the id and find the matching inner array. 

     //I then want to loop through the innerArray and append to my html a link using JQuery. 
     $('#articleLinks').append("<a href='"+innerArray[0]+"'>"+innerArray[1]+'</a>'); // use CSS to break lines 
     } 
} 

HTML:

<div id="articleLinks"> 
    <a href="http:/www.google.com">Example Link</a><br> 
</div> 

任何帮助将不胜感激。我试图尽可能简化&,以便它可读。

+0

与pastebin ...什么......有这么多事情发生。我点击了什么?我想要发生什么? – 2014-10-20 01:28:56

+0

顶级黑色条令人难以置信的烦人,顺便说一句。 – 2014-10-20 01:30:07

+1

有了所有这些代码,人们究竟做了什么来触发这个动作(你点击了什么),你想在点击上发生什么,以及它现在到底做了什么?你需要记住,我们对你想要做什么或你的页面如何工作一无所知。 – jfriend00 2014-10-20 01:30:15

回答

0

试试这个...

function myFunctionId (id) { 
    console.log(tubeArray); 
    tubeArray.forEach(function(entry) { 
     if (entry[0]==id) { 
     entry[4].forEach(function(innerArray){ 
      $('#articleLinks').append("<a href='"+innerArray[0]+"'>"+innerArray[1]+'</a>'); // use CSS to break lines 
     }); 
     return; 
     } 
    });     
} 

这使得它看起来像这样对我......你得来处理编码问题。与撇号。有很多的方法来处理它...

enter image description here

所以....如果是我......它不是。但如果是...我会使用关联数组而不是数字索引的数组,因为它更容易阅读代码并理解您正在使用的内容以及在何处,如何以及事物和内容。

tubeArray = { 
    'UQ' :  { 'location': [-27.495134, 153.013502], 
        'youtube': "example.com/embed/uZ2SWWDt8Wg", 
        'articles': [["example.com/queensland/brisbane-students-protest-university-fee-hikes-20140521-zrk8o.html", "Brisbane students protest university fee hikes"], 
           ["example.com/content/2012/s3578878.htm", "Angry protests over UQ student union election"], ] 
       }, 
    'New York': { 'location': [0.715520, -74.002036], 
        'youtube': "example.com/embed/JG0wmXyi-Mw", 
        'articles': [["example.com/2014/10/19/ny-taxpayers-risky-wall-street-bet-why-the-comptroller-race-matters/" , "NY taxpayers’ risky Wall Street bet: Why the comptroller race matters"]], 
       }, 
    } 
+0

我试图从字面上粘贴你的解决方案,它不起作用。 至于关联数组(它看起来好多了,我希望我最初可以这样做)。 – spamsaddle 2014-10-20 04:15:05

+0

@spamsaddle - 那个函数是我改变的唯一的东西,它对我来说确实有效。为什么它不适合你?错误还是别的?另外,你在什么浏览器?如果你在没有开发工具的IE中,请取出console.log()。 – 2014-10-20 04:28:09

+0

我刚测试过它......我重新复制了pastebin上的内容。更新了功能,没有别的。它显示的盒子和我在帖子中的图片完全一样。 – 2014-10-20 04:32:27