【问题标题】:add images dynamically created div to show text添加图像动态创建的 div 以显示文本
【发布时间】:2013-05-01 01:36:52
【问题描述】:

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

这是用 Flip 方法更新的 Fiddle http://jsfiddle.net/RQ62f/7/

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

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

请帮忙

【问题讨论】:

  • 运行jsfiddle时,找不到图片。

标签: jquery html css


【解决方案1】:

在 jQuery 中:

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

 });

一个元素必须存在才能附加一些东西。

【讨论】:

    【解决方案2】:

    试试这个,

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

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

    附:在您的小提琴中,您没有包含 jQuery 文件

    【讨论】:

      猜你喜欢
      • 2016-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多