添加图像动态创建div以显示文本

时间:2013-05-07 07:14:29

标签: jquery html css

onClick()图像事件我想动态创建div并将图像添加到该div。动态创建应该有背景颜色和一些文字请帮助。

这是使用翻转方法http://jsfiddle.net/RQ62f/7/

更新的小提琴
#foobar
{
    background-color: red;
content: div is here;   
}

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

请帮忙

2 个答案:

答案 0 :(得分:0)

在jQuery中:

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

 });

必须存在一个元素才能向其添加内容。

答案 1 :(得分:0)

试试这个,

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

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

P.S。在你的小提琴中,你没有包含jQuery文件

相关问题