問題7:色を取得して赤なら青にしよう
青色の中に1つだけ赤色のliタグがある
1つ1つクリックしていき、もし赤色だった場合
青色に変えよう。
1つ1つクリックしていき、もし赤色だった場合
青色に変えよう。
あらかじめエディタに書くコード
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
¥HTML¥
ul{
width: 1000px;
margin: 0 auto 0 auto;
display: flex;
list-style: none;
}
li{
height: 100px;
width: 100px;
background-color: #1d4177;
margin-right: 10px;
}
li:nth-child(1){background-color: rgb(0, 0, 255);}
li:nth-child(2){background-color: rgb(0, 0, 255);}
li:nth-child(3){background-color: rgb(255, 0, 0);}
li:nth-child(4){background-color: rgb(0, 0, 255);}
li:nth-child(5){background-color: rgb(0, 0, 255);}
¥CSS¥
期待する画面
解答ソースコード
$(function(){
$('li').on('click',function(){
if($(this).css('background-color') == 'rgb(255, 0, 0)'){
$(this).css('background-color', 'rgb(0, 0, 255)');
}
});
});
¥JavaScript¥
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="UTF-8">
<title>タイトル</title>
<meta name="viewport" content="width=device-width">
<style>
ul{
width: 1000px;
margin: 0 auto 0 auto;
display: flex;
list-style: none;
}
li{
height: 100px;
width: 100px;
background-color: #1d4177;
margin-right: 10px;
}
li:nth-child(1){background-color: rgb(0, 0, 255);}
li:nth-child(2){background-color: rgb(0, 0, 255);}
li:nth-child(3){background-color: rgb(255, 0, 0);}
li:nth-child(4){background-color: rgb(0, 0, 255);}
li:nth-child(5){background-color: rgb(0, 0, 255);}
</style>
</head>
<body>
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
<script
src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
</body>
</html>
¥HTML¥
完了にする!
活動記録をTweetする
1.JavaScriptでできること
もしその事を知らなくても、アラートやlogに出してみて
まずは一体どうゆう形で取得ができるのかを調べてみる
「#000」の形なのか「red」の形なのか。
憶測を立てつつ検証してみよう