【问题标题】:Add different class to each image with different id after drop event在 drop 事件后为每个具有不同 id 的图像添加不同的类
【发布时间】:2014-01-22 07:23:11
【问题描述】:

我在一个容器中有 5 张图像,我必须将它们放入另一个容器中才能完成图像。将图像放入另一个容器后,我想根据其 ID 向该图像添加不同的类。以便此图像片段将粘在此容器中存在的上一个图像上。

我能够实现拖放事件并在拖放事件后为每个图像添加一个类。我需要在我的代码中进行哪些更改以根据其 ID 为每个图像添加不同的类

我的代码

  <style>

   .add-style {
     position:absolute;
    }
   .north-style {
     width: 375px;top: 10px;left: 11px;

    }
   .south-style {
     width: 375px;top: 158px;left: 11px;

    }
    .east-style {
     width: 163px;top: 10px;left: 223px;height: 250px;

    }
    .west-style {
      width: 163px;top: 9px;left: 11px;height: 249px;
    }
    .center-style {
      width: 126px;top: 71px;left: 135px;
    }
 </style>

 <script>
   $(function() {

     $( "#sortable1" ).sortable({
        connectWith: "div"
       });

     $( "#sortable2" ).sortable({
        connectWith: "div",
        stop: function( event, ui ) {
           ui.item.addClass( "add-style" )

         }
     });

     $( "#sortable1, #sortable2" ).disableSelection();
    });
   </script>

代码主体部分

 <body>
   <div class="row-fluid" >
     <div class="span4">
       <div class="span1"></div>
       <div id="sortable1" class="well sidebar-nav sidebar-nav-fixed span11">

        <legend>Collect Coupon parts</legend>
        <span>1st part</span>
        <img id="north-img" class="ui-state-highlight" src="images/demo/north.png" >
        <span>2nd part</span>
        <img id="south-img" class="ui-state-highlight" src="images/demo/south.png" >
        <span>3rd part</span>
        <img id="east-img" class="ui-state-highlight" src="images/demo/east.png" >
        <span">4th part</span>
        <img id="west-img" class="ui-state-highlight" src="images/demo/west.png" >
        <span>5th part</span>
        <img id="center-img" class="ui-state-highlight" src="images/demo/center.png">

    </div>
  </div>
  <div id="sortable2"  class=" well sidebar-nav sidebar-nav-fixed span8">

    <legend>Canvas section</legend>
  </div>
  </div>
  </body>

【问题讨论】:

    标签: javascript jquery css jquery-droppable


    【解决方案1】:

    首先将您的课程更改为以下内容。

    显示两个示例,其他 3 个也更改。

         .north-img-style {
             width: 375px;top: 10px;left: 11px;
           }
         .south-img-style {
             width: 375px;top: 158px;left: 11px;
         }
    

    然后在这部分代码中添加这两行

         stop: function( event, ui ) {
    
              var theID = ui.item.attr('id');
              ui.item.addClass(theID + '-style');
    
         }
    

    var theID 获取元素的id,然后我们只需添加具有相同 ID 的类,但添加 -style 到它。所以这适用于你所有五个不同的班级。


    编辑

    是的,您一次可以添加多个课程。只需用逗号分隔,如下所示:

              var theID = ui.item.attr('id');
              ui.item.addClass(theID + '-style', 'add-style');
    

    【讨论】:

    • 但是有一个问题,我们必须第二次删除图像才能在第一次添加类 .north-img-style 时只添加添加样式类。是否可以在第一滴中添加两个类
    • 我仍然必须删除两次图像片段才能添加一个类。我认为在第一滴它得到 id 并在第二滴它调用.addclass() function
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-20
    • 2011-08-12
    • 2017-05-17
    • 2022-08-20
    • 2011-12-13
    • 2013-07-29
    相关资源
    最近更新 更多