jQuery divの背景に色を付けるサンプル
環境
Google Chrome 106.0.5249.119
Windows 10 Home 64bit
jQuery 3.6.0
構文
$('.divクラス名’).css('background-color’,色の値);
$('#divのid名’).css('background-color’,色の値);
「background-color」を利用してdivの背景に色を付けます
使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>div背景色変更サンプル</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btnchg").click(function(){
//div要素
$('.cft').css('background-color','green');
});
});
</script>
</head>
<body>
<div class="cft">test</div>
<p><button type="button" id="btnchg">変更</button></p>
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>div背景色変更サンプル</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#btnchg").click(function(){
//div要素
$('.cft').css('background-color','green');
});
});
</script>
</head>
<body>
<div class="cft">test</div>
<p><button type="button" id="btnchg">変更</button></p>
</body>
</html>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>div背景色変更サンプル</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ $("#btnchg").click(function(){ //div要素 $('.cft').css('background-color','green'); }); }); </script> </head> <body> <div class="cft">test</div> <p><button type="button" id="btnchg">変更</button></p> </body> </html>
結果
「変更」ボタンを押すと、div要素「cft」の背景に色(green)を付けます。