我有一個地方爲主照片(id =「main」)和表中的其他照片(id =「pic + 1(2,3,4,5,6) )「)。我想點擊一張id =「pic n」的照片,並將其加載到ID爲「main」的元素中。如何實現這一點?使用javascript在特定元素中加載圖像
另外我有一個錯誤
Uncaught TypeError: Cannot set property 'onclick' of null.
這裏是我的代碼:
<img id="main" src="">
galery(dir){
...
...
function createPreview() {
var f = document.createElement("table");
var row = f.insertRow();
for(var j = 0; j < count ; j++) {
var cell = row.insertCell(j);
var img = new Image();
img.src = dir + '/' + j + ".jpg";
img.width = "100";
img.id = 'pic' + j;
cell.appendChild(img);
}
document.body.appendChild(f);
}
document.getElementById(this.pic).onclick = function() {
document.getElementById('main').src = this.src;
}
...
}
尋求調試幫助的問題(「爲什麼這個代碼不工作?」)必須包含所需的行爲,特定的問題或錯誤以及在問題本身中重現問題所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。 – 2014-09-28 08:39:28
'this.pic'顯然不是你想象的那樣。 – adeneo 2014-09-28 08:41:03
幫幫我!我不知道該怎麼做。 – NEW 2014-09-28 08:42:03