class = "link"로 기존 링크를 복제하고 div "wrap"의 각 img 주위에 줄 묶는 방법?HTML의 기존 링크를 다른 이미지로 둘러 쌉니?
이$('#wrap img').wrap('<a href="http://foo.com"></a>');
HTML :
<a href="http://foo.com" class="link">
<img/>
</a>
<div id="wrap">
<img class="img" />
<img class="img" />
<img class="img" />
</div>
결과 :
<a href="http://foo.com" class="link">
<img/>
</a>
<div id="wrap">
<a href="http://foo.com" class="link"><img class="img" /></a>
<a href="http://foo.com" class="link"><img class="img" /></a>
<a href="http://foo.com" class="link"><img class="img" /></a>
</div>
'$ ('. link') .clone(). empty()'이면 충분하다. – dfsq
감사합니다. 둘 다 작동합니다! – coder
@ dfsq 그것도 작동합니다, jQuery는 그 경우 객체를 복제합니다. 원래 스 니펫은 문자열을 사용했기 때문에 'outerHTML'을 사용했습니다. – undefined