5
아래 코드에서 각 자식 노드를 반복하고 다른 요소에 자식을 추가하려고합니다. 루프 내부의 올바른 구문은 무엇입니까?jQuery는 각 자식 노드를 추가합니다
$(this).children().each(
$(div).appendChild(this.childNodes.length - 1);
);
아래 코드에서 각 자식 노드를 반복하고 다른 요소에 자식을 추가하려고합니다. 루프 내부의 올바른 구문은 무엇입니까?jQuery는 각 자식 노드를 추가합니다
$(this).children().each(
$(div).appendChild(this.childNodes.length - 1);
);
each()
함수 내에서 this
은 반복하는 대상을 가리 킵니다.이 경우 children()
입니다. 원래 jQuery 객체의 this
이 아닙니다. 따라서
:
$(this).children().each(function() {
$(div).appendChild($(this));
});
당신은 each
호출 함수 콜백 또는 익명 함수를 사용해야합니다 : 코드가 개선되었습니다 수없는 경우
$(this).children().each(function() {
$(div).appendChild(this.childNodes.length - 1);
});
또는
function doSomething() {
$(div).appendChild(this.childNodes.length - 1);
}
$(this).children().each(doSomething);
는 잘 모르겠어요하지만이 작은 부분 만 보았을 때 나는 거의 말할 수 없습니다.