jQuery :visibleで表示されている要素を取得する
環境
Google Chrome 111.0.5563.65(Official Build)
Windows 10 Home 64bit
jquery 3.6.3
書式
$(“タグ要素:visible").css(属性名,値);
「:visible」を使用して表示されている要素のスタイルシートを変更します
使用例
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#chk").click(function(){
$("p:visible").css("color","red");
});
});
</script>
</head>
<body>
<p>東京</p>
<p style="display:none;">大阪</p>
<p>横浜</p>
<button id="chk">検証</button>
</body>
</html>
実行結果
「検証」ボタンを押すと、表示されているp要素の文字を赤にします。