2013-05-07 27 views
0

onClick()事件圖片我想動態創建div並將圖片添加到該div。動態創建應該有背景顏色和一些文本請幫助。添加圖片動態創建div以顯示文字

此更新撥弄倒裝法http://jsfiddle.net/RQ62f/7/

#foobar 
{ 
    background-color: red; 
content: div is here; 
} 

in Jquery, 
$(this).appendTo('#foobar'); 

請幫

+0

當我運行jsfiddle時,圖像無法找到。 – Laughing 2013-05-07 07:21:55

回答

0

在jQuery中:

$("img").click(function(){ 
alert('m called'); 
if ($('#foobar').length==0) $('<div id="foobar"></div>').appendTo('body'); 
    $(this).appendTo('#foobar'); 

}); 

的元素必須存在,以追加什麼東西。

0

試試這個,

http://jsfiddle.net/RQ62f/8/

$("img").click(function(){ 
    $("body").append("<div id='foobar'></div>") 
     $(this).appendTo('#foobar'); 
}); 

附:在你的小提琴中,你沒有包含jQuery文件