2012-05-28 2 views
0

다음 코드를 작성한 후 해당 포인트 (img 및 div 내의 텍스트)가 생성 된 후에 드래그 할 수있게하려고합니다.jQuery-UI를 드래그 할 수없는 이유는 무엇입니까?

<div id="container"> 
    <img src="images/aoi.png" alt="" /> 
    <div id="point_panel"> 
     <form> 
      <fieldset id="point_container"> 
       <img id="point" src="images/point.png" alt="" /><input id="point_name" type="text" /> 
      </fieldset> 
     </form> 
    </div> 
</div> 
<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#point').click(function() { 
      var alt = $('#point_name').val(); 
      $('#container').append('<div class="points"><img src="images/point.png" alt=\"'+alt+'\" />'+alt+'</div>'); 
     }); 
     $('.points').draggable({ containment: '#container', stack: '.points', opacity: 0.5, zIndex: 100 }); 
    }); 
</script> 

양식에서 img를 클릭하면 동일한 img 및 텍스트를 가진 새 div가 만들어집니다. 그런 다음 div는 컨테이너의 ID를 사용하여 div 내에서 드래그 할 수 있어야합니다.

포인트를 만들면 드래그 할 수 없습니다. 내 암호가 없습니까?

+1

당신의 .points 요소도 존재하기 전에 당신은 드래그 이벤트를 바인딩, 그래서 이벤트가 아무것도 바인더 제본되지 않습니다. – roflmao

+0

당신 말이 맞아요. 그것은 그것을 고쳤다. 감사. – ahhchuu

답변

1

이 시도

감사 :

$(document).ready(function() { 
    $('#point').click(function() { 
     var alt = $('#point_name').val(); 
     var el = $('<div class="points"><img src="images/point.png" alt=\"'+alt+'\" />'+alt+'</div>'); 
     $('#container').append(el); 
     $(el).draggable({ containment: '#container', stack: '.points', opacity: 0.5, zIndex: 100 }); 
    }); 
}); 
+0

굉장합니다. 고마워요. 4 분 안에 답변하도록 설정됩니다. : P – ahhchuu

+0

추가 메모와 마찬가지로. "포인트"HTML을 변수 el로 설정하고 변수 el에 드래그 가능한 바인드를 발행하는 이유는 모든 ".points"요소에 대한 바인딩보다 빠르기 때문입니다. jQuery의 클래스 선택기는 강력하지만 상대적으로 느리기 때문에 가능하면 피하는 것이 가장 좋습니다. – roflmao

+0

잘 알고 있습니다. 나는 아직도 jquery에 비교적 새로운 편이다. 다시 한번 감사드립니다. – ahhchuu

1
$(document).ready(function() { 
      $('#point').click(function() { 
       var alt = $('#point_name').val(); 
       $('#container').append('<div class="points"><img src="images/point.png" alt=\"'+alt+'\" />'+alt+'</div>'); 
       //bind draggable to last inserted div 
       $('#container').find('.points:last').draggable({ containment: '#container', stack: '.points', opacity: 0.5, zIndex: 100 });    
      }); 
      $('.points').draggable({ containment: '#container', stack: '.points', opacity: 0.5, zIndex: 100 }); 
     }); 
+0

고마워요. roflmao의 해결책은 좀 더 깔끔해서 나는 그걸로 갈 것이다. – ahhchuu

관련 문제