2013-02-14 2 views
4

jQuery의 clone 기능은 다음과 같습니다이벤트 핸들러가 아닌 데이터를 복사하여 노드를 복제하는 방법은 무엇입니까?

.clone([withDataAndEvents ] [, deepWithDataAndEvents ])

withDataAndEvents: A Boolean indicating whether event handlers and data should be copied along with the elements.

deepWithDataAndEvents: A Boolean indicating whether event handlers and data for all children of the cloned element should be copied.

이러한 매개 변수 모두 dataevents에 영향을 미친다. 데이터 만 유지할 수는 있지만 이벤트는 유지할 수 없습니까? 기본적으로, 나의 이상적인 clone 기능은 다음과 같을 것이다 :이 acheive 수있는 방법이

.idealClone([withData] [, withEvents ] [, deepWithData ] [, deepWithEvents ])

withData: A Boolean indicating whether data should be copied along with the elements.

withEvents: A Boolean indicating whether event handlers should be copied along with the elements.

deepWithData: A Boolean indicating whether data for all children of the cloned element should be copied.

deepWithEvents: A Boolean indicating whether event handlers for all children of the cloned element should be copied.

있습니까?

NB 나는 1.8.1와 함께 일하고 있어요,하지만 모든 버전에 대한 대답은 쉽게 당신의 이상적인 복제 방법으로 jQuery 오브젝트를 확장 만 유지 dataoff의 다양한 조합을 사용할 수 있습니다

+1

방금'클론()'와'언 바인드()'를 사용할 수 없습니다 : 개인적으로 나는 그냥 할 정도로 쉽게 생각? – BenM

답변

3

환영합니다 네가 원하는 것들.

// clones with data and events, then unbinds all events (bound with .on) 
$(element).clone(true).off(); 
+0

'on()'으로 묶여있는 것들만 제거하지 않겠습니까? – BenM

+0

@ 벤 예. 나는 그렇게 생각한다. 나는 시도하지 않았지만, 문서는 그렇게 제안한다. – nbrooks

+0

모든 이벤트는 최신 jQuery 버전에서'.on()'으로 첨부됩니다 (1.7보다 크다고 생각합니다). 예를 들어'.click (handler)'를 사용하는 것은'.on ('click', handler)'를 사용하기위한 지름길이기 때문에,'.bind()'를 사용하여 어떤 메소드를 연결하지 않고'.off '괜찮을거야! – jpatiaga