Apply jQueryUI Resizable widget to dynamically created elements

前端 未结 3 1739
忘了有多久
忘了有多久 2020-12-22 00:45

This question is not about event delegation

I would like to apply the jQuery resizable widget to a series of div tags that could potentially be create

3条回答
  •  执念已碎
    2020-12-22 01:09

    You need to reinitialize col.resizable(); once new element added. Please check below working code:

    $(function() {
      var cols = $(".col");
    
    
      cols.resizable({
        maxHeight: 20,
        minHeight: 20,
        distance: 5,
        handles: 'e',
        start: function() {
          console.log("I've started!");
        },
        stop: function() {
          console.log("I've stopped!");
        }
    
    
      });
    
    
      $("#addNew").on("click", function() {
    
        cols.filter(":last").after('
    ' + parseInt(cols.length + 1) + '
    '); cols = $(".col"); cols.resizable({maxHeight: 20, minHeight: 20, distance: 5, handles: 'e'}); }); });
    .col {
      float: left;
      width: 20px;
      height: 20px;
      background: #f00;
      margin-right: 1px;
    }
    .col.new {
      background: #0f0;
    }
    button {
      clear: left;
      display: block;
      margin-top: 10px;
    }
    
    
    
    
    
    1
    2
    3
    4

提交回复
热议问题