在jQuery中将动态添加的元素作为可排序句柄

时间:2017-02-15 12:44:10

标签: javascript jquery

我有以下代码。在悬停div时,我添加一个按钮,并希望将该按钮设为拖动手柄。但由于某种原因,阻力似乎不起作用。我做错了什么?



$(document).ready(function() {
  var active = 0;
  jQuery(document).on("mouseover", ".mfx-container", function(e) {
    if (active == 0) {
      active = 1;
      jQuery(".mfx-container").css("outline", "none");
      jQuery(this).css("outline", "2px solid #DDE5EC");
      jQuery(".editor-hover-buttons").remove();
      jQuery(this).append('<button class="editor-hover-buttons move-button" type="reset">Move</button>');

      jQuery(".mfx-wrapper").sortable({
        handle: ".move-button"
      });


    }
  })
  jQuery(document).on("mouseout", ".mfx-container", function(e) {
    if (active == 1) {
      active = 0;
    }
  })




})
&#13;
.mfx-container {
  min-height: 100px;
  position: relative;
}
.delete-block-button {
  position: absolute;
  top: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<div class="mfx-wrapper">
  <div class="container-fluid mf-wpt1-banner mfx-container">
    <span class="strong"> Introducing Marketfox!</span> You can now increase conversions on any web page
  </div>
  <div class="container mfx-container">
    <h2 class="text-center mt30 mb30">Lorem ipsum dolor sit amet</h2>
    <div class="col-sm-6 lh2 mb30">
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus bibendum magna turpis. Aliquam ultricies interdum risus, ut interdum justo imperdiet eget.
    </div>
    <div class="col-sm-6 lh2 mb30">
      Vivamus in placerat odio. Mauris nec augue vitae est facilisis tincidunt. Donec quis commodo mi, quis porta magna. Sed laoreet id lectus ac interdum.
    </div>
    <div class="col-sm-6 lh2 mb30">
      Vivamus in placerat odio. Mauris nec augue vitae est facilisis tincidunt. Donec quis commodo mi, quis porta magna. Sed laoreet id lectus ac interdum.
    </div>
    <div class="col-sm-6 lh2 mb30">
      Vivamus in placerat odio. Mauris nec augue vitae est facilisis tincidunt. Donec quis commodo mi, quis porta magna. Sed laoreet id lectus ac interdum.
    </div>

  </div>

  <div class="container mfx-container">
    <div class="col-sm-4 p30">
      <img class="img-responsive" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150">
    </div>
    <div class="col-sm-4 p30">
      <img class="img-responsive" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150">
    </div>
    <div class="col-sm-4 p30">
      <img class="img-responsive" src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

首先,正如gpgekko指出的那样,move-block-button很可能是move-button的错字。

其次,jQuery UI可排序似乎不能与button一起使用。

如果您将button更改为div,那就可以了。有关此演示,请参阅http://jsbin.com/kiyozutodo/edit?html,js,output