Using data attributes is easy once you get the idea.
css
.redStuff, .blueStuff, .greenStuff {
display: none;
}html
<ul id="color">
<li id="red" data-color="red"><a href="#">Red</a></li>
<li id="blue" data-color="blue"><a href="#">Blue</a></li>
<li id="green" data-color="green"><a href="#">Green</a></li>
</ul>
<div class="redStuff" data-content="red">Red Stuff</div>
<div class="blueStuff" data-content="blue">Blue Stuff</div>
<div class="greenStuff" data-content="green">Green Stuff</div>jquery
// no need for the ids or classes
// we set data attributes for the html
$("li[data-color]").click(function(){
// next line is for second click, to hide the prev div element
$("div[data-content]").hide();
// we are getting the data-color attr value here
// and for readibility we assigned it to a variable called color
var color = $(this).data("color");
// find the div with the same content and show
$("div[data-content='"+color+"']").show();
});'jscript' 카테고리의 다른 글
| PHP로 간단하게 모바일 기기 체크하기 (0) | 2018.02.09 |
|---|---|
| 속성, 내용, 스타일, 데이터 조회 및 변경 (0) | 2018.02.08 |
| Toggle between hiding and showing the <p> element when you click on the "Toggle" button. (0) | 2018.01.31 |
| [jQuery] 검색된 요소 크기 구하기. [ .width(), .height(), .outerWidth(), .outerHeight() ] (0) | 2018.01.29 |
| 제이쿼리로 css값 변경하기 (0) | 2018.01.29 |