2014-12-25 3 views
2

부모 요소에서 .children을 사용하고 $ (this)를 사용하여 이벤트 처리기로 특정 자식 요소 만 참조하는 방법이 있는지 궁금합니다. 이처럼 : 나 여기에 펜을 참조하십시오 http://codepen.io/coralsea/pen/JoRrRG

<div class="petals"> 
    <div class="petal-1"> 
    </div> 
    <div class="petal-2"> 
    </div> 
    <div class="petal-3"> 
    </div> 
    <div class="petal-4"> 
    </div> 
    <div class="petal-5"> 
    </div> 
    <div class="petal-6"> 
    </div> 
    <div class="petal-7"> 
    </div> 
</div> 

/

$(document).ready(function() { 
    $('.petals').children().draggable() 
    $(this).mouseup(function() { 
    $(this).animate({ 
    backgroundColor: 'red', 
    top: "+=50" 
    }, 2000, function() { 

    }); 
}); 
}); 

나는 각 자녀에 클래스를 추가하고 대상으로 작업 할 수 있었다과 같이, 그 : http://codepen.io/coralsea/pen/emdGmo이 있지만 수있는 것 부모의 자녀를 대상으로하는 것이 더 효율적입니다.

+0

다른 사람들이 지적했듯이 ,'$ (". petals")를 사용할 수있다. .children()'을 사용하지만 꽃잎을'$ ('. petal')'로 직접 타겟팅하면 특별히 잘못된 것은 없습니다. 그 접근 방식에 대해 당신은 무엇을 좋아하지 않습니까? – JLRishe

+0

꽃잎 종류가 스타일에 필요하지 않으므로 html로 추가 클래스를 추가합니다 –

답변

1

다른 방법 ($(this) 부분 제거) :

$(document).ready(function() { 
    $('.petals').children().draggable({ 
     stop: function (event, ui) { 
      $(this).animate({ 
       backgroundColor: 'red', 
       top: "+=50" 
      }, 2000, function() { 
       // Animation complete. 
      }); 
     } 
    }); 
}); 

CodePen link

+0

그게 완벽합니다. 고맙습니다! –

+0

@ user3209044이 답변이나 여기에있는 다른 답변으로 문제가 해결되면 체크 표시를 클릭하여 수락을 고려하십시오. 이는 해결책을 찾았고 응답자와 자신에게 어느 정도의 평판을 제공한다는 것을 더 넓은 커뮤니티에 나타냅니다. 이를 수행 할 의무는 없습니다. :) –

+0

나는 사랑하고 싶지만 나의 명성은 아직 충분하지 않다. 당신의 도움을 주셔서 감사합니다. –

1

명령을 연결할 수 있습니다. jQuery를 UI의 stop 이벤트를 사용하여 일을

$(document).ready(function() { 
    $('.petals') 
     .children() 
     .draggable() 
     .mouseup(function() { 
     $(this).animate({ 
      backgroundColor: 'red', 
      top: "+=50" 
     }, 2000, function() { 
      // Animation complete. 
     }); 
    }); 
}); 

http://codepen.io/gpetrioli/pen/WbGZog

+0

이것은 나를 위해 일했습니다. 감사합니다. –

관련 문제