首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将图像添加到div并使其“活动”

问将图像添加到div并使其“活动”
EN

Stack Overflow用户
提问于 2015-10-09 10:18:30
回答 1查看 55关注 0票数 1

我只是在学习jQuery,并试图通过多种方法解决这个问题,太多的方法无法列出,但没有成功。请帮帮我!

另外,我不知道如何将图像添加到这个问题中,所以选择一个最喜欢的猫图像并修改代码以反映其名称。(对不起)

指令:

  1. 将html代码和图像加载到同一个目录中。
  2. 在浏览器中显示simple.html (我正在使用Chrome)
  3. 点击图片-它变大了
  4. 点击大图片-它变小了。
  5. 点击“添加图像”按钮-一个新的图像出现
  6. 单击第一个图像-它变大并隐藏第二个图像。
  7. 点击大图像-它变小了,第二个图像也会显示出来。
  8. 点击第二个图像-什么都没有发生,我希望它变大(第二次点击返回到小)

代码:

代码语言:javascript
复制
    <!doctype html>
    <html lang="en">
    <head>
       <meta charset="utf-8">
       <title>simple demo</title>
       <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
    </head>
    <body>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <!-- DOM follows-->
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
       <div id="button">Add images</button></div>
       <div class="image_set" id="images">
          <img class="floating-box" src="test_image.jpg">
          <img class="floating-box" src="test_image.jpg">
       </div>
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <!-- end of DOM -->
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
       <script>
             $(".floating-box").click(function(){
                if(typeof floatingBoxMode == "undefined"){
                   floatingBoxMode = "full";
                }
                if (floatingBoxMode == "full"){
                   // need to set image css to fullsize
                   $(this).css("width", "auto");
                   $(this).css("height", "auto");
                   $(".floating-box").hide ();
                   $(this).show ();
                   floatingBoxMode = "single";
                }else{
                   // need to set css to thumbnail
                   $(this).css("width", "auto");
                   $(this).css("height", "150px");
                   $(this).show ();
                   $(".floating-box").show ();
                   floatingBoxMode = "full";
                };
             });
             $("#button").click (function () {
                var s = '<img class="floating-box" src="test_image.jpg">';
                $("#images").append (s);
             });
    </script>
    <style type="text/css">
    .floating-box {
        float: left;
        width: auto;
        height: 150px;
        margin: 10px;
        border: 3px solid #8AC007;  
        max-width: 100%;
        max-height: 80%;
    }
    .floating-box-fullsize {
        float: left;
        width: 100%;
        height: 100%;
        margin: 10px;
        border: 3px solid #8AC007;  
    }
    .image_set{
    margin: 10px; /*Margins for inner DIV inside each column (to provide padding)*/
    margin-top: 0;
    }
    </style>
    </body>
    </html>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-09 10:26:01

只管写

代码语言:javascript
复制
$('.image_set').on('click','.floating-box',function(){

而不是

代码语言:javascript
复制
$(".floating-box").click(function(){

动态添加元素需要event delegation来激发它们的事件

这是演示

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33035695

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档