목차
- 특정 글자를 포함하는 요소의 글자색 변경
- 특정 글자와 일치하는 요소만 글자색 변경
특정 글자를 포함하는 요소의 글자색 변경
<p>홈짱</p>
<p>닷컴</p>
<p>홈짱닷컴</p>
<script>
document.querySelectorAll('p').forEach(p => {
if (p.textContent.includes("홈짱")) {
p.style.color = 'red';
}
});
</script>
결과보기
특정 글자와 일치하는 요소만 글자색 변경
<p>홈짱</p>
<p>닷컴</p>
<p>홈짱닷컴</p>
<style>
document.querySelectorAll('p').forEach(p => {
if (p.textContent.trim() === "홈짱") {
p.style.color = 'red';
}
});
</style>
결과보기