ログイン
問題7

色を取得して赤なら青にしよう

アイコン画像

問題7:色を取得して赤なら青にしよう

青色の中に1つだけ赤色のliタグがある
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¥
            

コメントのアイコン解説

色は「rgb」で取得ができる。
もしその事を知らなくても、アラートやlogに出してみて
まずは一体どうゆう形で取得ができるのかを調べてみる
「#000」の形なのか「red」の形なのか。
憶測を立てつつ検証してみよう
ログインして解答を見る
完了にする!
twitterのアイコン
活動記録をTweetする
1.JavaScriptでできること
続きの動画を見たい方は公式LINEから「JavaScript」と送信すると動画が見れます。
JavaScriptを学ぶなら現役エンジニア監修「甲賀コース」
LINEの友達追加でお役立ち動画をGET!!

閉じる