2012-12-25 85 views
-1

我有JSON包含圖像。它看起來像:getJSON,迭代,顯示圖像

["1.jpg","2.jpg","3.jpg","4.jpg","Thumbs.db","..","." .............] 

我可以用一個像訪問它們一個:

data[0] 
data[1] 
data[2] 

不過,我想在一個特定的div動態顯示所有這些圖像一個以上的其他(一列) 。事情是這樣的:

$(document).ready(function() { 

    updateProfil(); 

    function updateProfil() { 
     $.getJSON("./index_logg1.php", null, processCategory); 
    } 

    function processCategory(data) { 
     $.each(data, function() { 
      // the code ???? 
     }); 

     $("#my_user").html('<img src='+ ???? +' />'); 
     $("#my_user").hide().fadeIn(700); 
    } 

}); 

編輯:

嗨,

感謝您的答覆。 我現在可以動態可視化圖像(使用下面的代碼),但問題是我希望能夠捕獲(動態)單擊圖像時的href值。 (然後送它通過jQuery後到PHP)

它顯示在鼠標懸停的值,但他們捕捉時clicked.Nothing似乎到目前爲止是工作沒有運氣...

代碼:

enter code here 
    function updateProfil() { 
    $.getJSON("./index_logg2.php", null, processCustom); 
    } 

function processCustom(data) { 
    $.each(data, function(k, v) { 
    $(".panel").append('<center><a href="?cust='+ k +'"><img src="images/custom/'+ v +' "title="Click to set it" "></a></center><br />'); 

    /* 
    $(".panel").click(function() { 
var data= ??? 
    $.post("./index_logg2.php", { data: ??? }, updateProfil); 
    }); 
    */ 

    }); 
    } 

謝謝!

+0

而問題會是? –

+0

查看jQuery教程和API文檔。它包含有關如何創建和添加元素的所有信息:http://docs.jquery.com/Tutorials,http://api.jquery.com。 –

回答

0

可以使用this$.each內,

function processCategory(data) { 

    $.each(data, function(){ 
     $("#my_user").append("<img src='"+this+"' />"); 
    }); 

    $("#my_user").hide().fadeIn(700); 
} 
0
$(document).ready(function() { 

    updateProfil(); 

    function updateProfil() { 

    html=""; 
    $.getJSON('./index_logg1.php', function(data) { 

     $.each(data, function(i) { 
      html+='<img src='+ data[i] +' />' 
     }); 

     $("#my_user").html(html); 
     $("#my_user").hide().fadeIn(700); 
    } 

}); 
0

我不知道這是具有最佳性能的解決方案,但加上一個其他的影像,確保瞬時加入之後。

function processCategory(data) { 
     $.each(data, function(i, val) { 
     $('#my_user').append($('<img>', { src: val, alt: 'pic' })); 
     }); 

     $("#my_user").hide().fadeIn(700); 
    }