index.html的Pastebin:http://pastebin.com/g8WpX6Wn(此工作但有一些中斷的img鏈接&沒有CSS)。在JavaScript中訪問數組內的數組中的元素
Zip文件,如果你想看到整個項目:
我試圖動態改變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.
for (j = 0; i < innerArray.length; j++){
//supposed to get "www.linkX.com"
var $link = ;
//supposed to get "titleX"
var $title = ;
//change the content of <div id="articleLinks">
$('#articleLinks').append('<a href=$link>$title</a><br>');
}
}
HTML:
<div id="articleLinks">
<a href="http:/www.google.com">Example Link</a><br>
</div>
任何幫助將不勝感激。我試圖儘可能簡化&,以便它可讀。
它幫助我更好地形象化,但我不知道如何解決問題。我希望能夠在tubeArray中搜索id。 – Spamsational 2014-10-19 23:31:20
對我的答案進行編輯。 – 2014-10-19 23:32:25
我應該編輯什麼? – Spamsational 2014-10-19 23:36:43