jQuery findメソッドで子要素を検索して操作する方法
環境
Google Chrome 111.0.5563.65(Official Build)
Windows 10 Home 64bit
jquery 3.6.3
書式
親要素.find( selector/子要素 )
jQueryのfindメソッドは、指定の要素から子孫要素を検索できます。
$(“.cft").find(“.pA").css(“color", “red");
指定の要素を起点に子要素を検索し一致した子要素の文字の色を赤にします。
使用例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btnchk").click(function () {
$(".cft").find(".pA").css("color", "red");
});
});
</script>
</head>
<body>
<div class="cft">
<p>tokyo</p>
<p class="pA">oosaka</p>
</div>
<input type="button" id="btnchk" value="確認" />
</body>
</html>
実行結果
1行目の「cft」を起点にして、findメソッドでその配下の3行目の子要素の「pA」を指定して文字の色を赤にします。