2017-02-15 64 views
0

我有以下代碼。在懸停的div我添加一個按鈕,並希望使該按鈕作爲拖動手柄。但由於某種原因,這種阻力似乎並不奏效。我究竟做錯了什麼?在jQuery中動態添加元素作爲可排序句柄

$(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; 
 
    } 
 
    }) 
 

 

 

 

 
})
.mfx-container { 
 
    min-height: 100px; 
 
    position: relative; 
 
} 
 
.delete-block-button { 
 
    position: absolute; 
 
    top: 10px; 
 
}
<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>

+0

你的按鈕有class'move-button',你的手柄是'move-block-button'? – gpgekko

+0

這是一個錯字。問題依然存在。 –

+0

可能重複的[JQuery UI可排序,按鈕不能作爲句柄](http://stackoverflow.com/questions/12142827/jquery-ui-sortable-button-is-not-working-as-handle) –

回答

1

首先,gpgekko指出,move-block-button很可能是move-button一個錯字。

其次,jQuery UI可排序似乎不適用於button作爲其句柄。

如果您將button更改爲div,那麼它可以工作。請參閱http://jsbin.com/kiyozutodo/edit?html,js,output瞭解相關演示。

+0

你可以如果您將'cancel:'''添加到可排序的呼叫中,請使用按鈕作爲句柄 –