Created
December 3, 2018 14:01
-
-
Save kosh-jp/860f5f054d0493a53b3e131fb2caf8d3 to your computer and use it in GitHub Desktop.
jQuery/content-show-hide-by-a-tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="ja"> | |
<head> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> | |
</head> | |
<body> | |
<nav class="navbar navbar-expand-lg navbar-light bg-light"> | |
<a class="navbar-brand" href="#">Navbar</a> | |
<div class="collapse navbar-collapse" id="navbarNav"> | |
<ul class="navbar-nav"> | |
<li class="nav-item"> | |
<a class="nav-link" data-link="syake" href="#">鮭</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" data-link="ume" href="#">梅</a> | |
</li> | |
<li class="nav-item"> | |
<a class="nav-link" data-link="okaka" href="#">おかか</a> | |
</li> | |
</ul> | |
</div> | |
</nav> | |
<div class="main-content"> | |
<div data-content="syake"> | |
<h1>syake page</h1> | |
</div> | |
<div data-content="ume"> | |
<h1>ume page</h1> | |
</div> | |
<div data-content="okaka"> | |
<h1>okaka page</h1> | |
</div> | |
</div> | |
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> | |
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> | |
<script type="text/javascript" src="content-show-hide.js"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function () { | |
select("syake"); | |
$("#navbarNav a").on("click", function() { | |
var $filling = $(this).data("link"); | |
select($filling); | |
}); | |
function select ($filling) { | |
$(".main-content > div").each(function() { | |
if ($(this).data("content")!==$filling) { | |
$(this).hide(); | |
} else { | |
$(this).show(); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment