제이쿼리 replace 질문 채택완료

<div id="hello">

<table>

<thead>

<tr>

<td>ab a</td>

<td>b a a</td> 

<td>caa</td> 

</tr>

</thead>

<tbody>

<tr>

<td>a</td>

<td>b</td> 

<td>c</td> 

</tr>

</tbody>

</table>

<ul>

<li>a</li>

<li>b</li> 

<li>c</li> 

</ul>

</div>

 

 

#hello 안에 있는 모든 a 를 b 로 바꾸고 싶습니다.. 도움부탁드립니다

답변 1개

채택된 답변
+20 포인트

$('#hello td, #hello li').each( function() {

    $(this).text( $(this).text().replace(/a/g, 'b') );

})


로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

감사합니다 $('#hello td, #hello li') 에서 td랑 li를 딱히 안정해주고 #hello의 모든 자식에 대해 하려면 어떻게해야 하나요?

$(#hello).find().each(function() { 로 해보니 find에 자식을 정해줘야 실행이됩니다
// 정규식은 구문을 보고 패턴을 만드는 것이기 때문에 아래 코드는 참고만 하세요
$('#hello').find('*').each( function() {
$(this).html( $(this).html().replace(/a/g, 'b') );
})

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인
🐛 버그신고