js010.html ๋ณต์ฌํด์ js011.html ํ์ผ ์์ฑ ํ
body ํ๊ทธ ์์ ๋ด์ฉ์ ์ ๋ถ ์ญ์
(bootstrap, jquery cdn์ ๋จ๊ฒจ๋์)
Tab UI๋ฅผ ๋ง๋ค์ด ๋ณด์
Copy <! doctype html >
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< title >Bootstrap demo</ title >
< link href = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/css/bootstrap.min.css" rel = "stylesheet" integrity = "sha384-4bw+/aepP/YC94hEpVNVgiZdgIC5+VKNBQNGCHeKRQN+PtmoHDEXuppvnDJzQIu9" crossorigin = "anonymous" >
< link rel = "stylesheet" href = "main.css" >
< script src = "https://code.jquery.com/jquery-3.7.0.min.js" integrity = "sha256-2Pmvv0kuTBOenSvLm6bvfBSSHrUJ+3A7x6P5Ebd07/g=" crossorigin = "anonymous" ></ script >
< style >
ul .list {
list-style-type : none ;
margin : 0 ;
padding : 0 ;
border-bottom : 1 px solid #ccc ;
}
ul .list::after {
content : '' ;
display : block ;
clear : both ;
}
.tab-button {
display : block ;
padding : 10 px 20 px 10 px 20 px ;
float : left ;
margin-right : -1 px ;
margin-bottom : -1 px ;
color : grey ;
text-decoration : none ;
cursor : pointer ;
}
.orange {
border-top : 2 px solid orange ;
border-right : 1 px solid #ccc ;
border-bottom : 1 px solid white ;
border-left : 1 px solid #ccc ;
color : black ;
margin-top : -2 px ;
}
.tab-content {
display : none ;
padding : 10 px ;
}
.show {
display : block ;
}
</ style >
</ head >
< body >
< div class = "container mt-5" >
< ul class = "list" >
< li class = "tab-button" >Products</ li >
< li class = "tab-button orange" >Information</ li >
< li class = "tab-button" >Shipping</ li >
</ ul >
< div class = "tab-content" >
< p >์ํ์ค๋ช
ํญ Product</ p >
</ div >
< div class = "tab-content show" >
< p >์คํ์ค๋ช
ํญ Spec</ p >
</ div >
< div class = "tab-content" >
< p >๋ฐฐ์ก์ ๋ณด ํญ Shipping</ p >
</ div >
</ div >
< script >
</ script >
< script src = "https://cdn.jsdelivr.net/npm/bootstrap@5.3.1/dist/js/bootstrap.bundle.min.js" integrity = "sha384-HwwvtgBNo3bZJJLYd8oVXjrBZt8cqVSpeBNS5n7C8IVInixGAoxmnlMuBnhbgrkm" crossorigin = "anonymous" ></ script >
</ body >
</ html >
products ๋ฒํผ์ ๋๋ฅด๋ฉด ์ํ ์ค๋ช
ํญ์ด ๋ณด์ด๊ณ , information๋ฒํผ์ ๋๋ฅด๋ฉด ์คํ์ค๋ช
ํญ์ด ๋ณด์ด๊ฒ ๋ง๋ค๋ฉด ๋๋ค.
๐ javascript ๋ชจ๋ํ
css ํ์ผ์ main.css๋ก ๋ฐ๋ก ๋บ ๊ฒ ์ฒ๋ผ js๋ ๋ชจ๋ํ ํ ์ ์๋ค.
Copy <! DOCTYPE html >
< html lang = "en" >
< head >
< meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" >
< meta charset = "utf-8" >
< meta http-equiv = "X-UA-Compatible" content = "IE=edge" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< meta name = "description" content = "Blog about personal projects written mostly in JavaScript with AngularJS and Node.js. MongoDB is a guest too." >
< meta name = "author" content = "Kevin Delemme" >
< meta name = "fragment" content = "!" >
< title >Kevin Delemme - JavaScript Addict - Personal projects written mostly with AngularJS, Node.js and MongoDB</ title >
< link href = "dist/css/bootstrap.min.css" rel = "stylesheet" >
< link href = "dist/css/style.css" rel = "stylesheet" >
< link href = "dist/css/font-awesome.min.css" rel = "stylesheet" >
< link href = "lib/wysiwyg/css/bootstrap3-wysiwyg5.css" rel = "stylesheet" >
</ head >
< body ng-app = "app" >
< div class = "container col-sm-12" >
< header class = "header" >
< h1 class = "header-title" >
< a href = "#" >
< i class = "fa fa-code" ></ i > Kevin Delemme
</ a >
</ h1 >
</ header >
< div ng-view ></ div >
</ div >
< script src = "lib/angular/angular.min.js" ></ script >
< script src = "lib/angular/angular-route.min.js" ></ script >
< script src = "lib/jquery/jquery-2.0.3.min.js" ></ script >
< script src = "lib/bootstrap/bootstrap.min.js" ></ script >
< script src = "dist/js/blog.js" ></ script >
< script src = "lib/wysiwyg/js/wysihtml5-0.3.0.min.js" ></ script >
< script src = "lib/wysiwyg/js/bootstrap3-wysihtml5.js" ></ script >
< script >
( function (i , s , o , g , r , a , m){i[ 'GoogleAnalyticsObject' ] = r;i[r] = i[r] ||function (){
(i[r].q = i[r].q || []) .push (arguments)} , i[r].l = 1 *new Date ();a = s .createElement (o) ,
m = s .getElementsByTagName (o)[ 0 ]; a .async = 1 ; a .src = g; m . parentNode .insertBefore (a , m)
})(window , document , 'script' , '//www.google-analytics.com/analytics.js' , 'ga' );
ga ( 'create' , 'UA-46674011-1' , 'kdelemme.com' );
ga ( 'send' , 'pageview' );
</ script >
</ body >
</ html >
js ํด๋๋ฅผ ์์ฑํ๊ณ app.js ํ์ผ ์์ฑ ํ
Copy <script src="js/app.js"></script>
html์ <body>ํ๊ทธ ์ ์ผ ์๋์ ์ด๋ถ๋ถ ์ฝ์
ํ๋ฉด app.js์ ์คํฌ๋ฆฝํธ ์ฌ์ฉ ๊ฐ๋ฅ
์ ์ด๋ ๊ฒ ํ ๊น?
์ด๋ฒ์ ๊ฐ๋ฐํ๋ ํญ UI๋ฅผ ๋ชจ๋ ํ์ด์ง๋ง๋ค ์ฌ์ฉํ๋ค๊ณ ๊ฐ์ ํ๋ฉด
๋ชจ๋ html ํ์ผ์ ๋๊ฐ์ js๋ฅผ ๋ณต๋ถํด์ผํ๋ค.
์ด๋ฐ ๊ท์ฐฎ์์ ๋ฐฉ์งํ๊ณ ์ ๋ชจ๋ํํด์ ์ฌ์ฉํ๋ค.
๋ชจ๋ํ๋ฅผ ์ํ๋ ๊ฐ๋ฐ์๊ฐ ์ง์ง ์ค๋ ฅ์๋ ๊ฐ๋ฐ์๋ค.
๋ค์ ๋์์์
products ๋ฒํผ์ ๋๋ฅด๋ฉด ์ํ ์ค๋ช
ํญ์ด ๋ณด์ด๊ฒ ํด๋ณด์
products ๋ฒํผ์ ๋๋ฅด๋ฉด ๋ฒํผ์ orange ํด๋์ค ์ถ๊ฐ
product div ์ show ํด๋์ค ์ถ๊ฐ
์ด๋ ๊ฒ ํ๋ฉด ๋ฒ๊ทธ ์๊น๐ฅ
๋ชจ๋ ๋ฒํผ์ orange ํด๋์ค์ div show ํด๋์ค ์ ๊ฑฐ
jQuery๋ก class ์
๋ ํธ ํ๋ฉด document.querySellectAll๊ณผ ๊ฐ์ด ๋์ ํจ
$('.ํด๋์ค๋ช
').eq(0) ํ๋ฉด document.querySellectAll('.ํด๋์ค๋ช
')[0] ๊ณผ ๊ฐ์ ๋์
โจ ๊ณผ์ . ๋ฒํผ 3๊ฐ์ ํญ ๊ธฐ๋ฅ ๊ตฌํ ํด๋ณด๊ธฐ
Copy $ ( '.tab-button' ) .eq ( 0 ) .on ( 'click' , function (){
$ ( '.tab-button' ) .removeClass ( 'orange' );
$ ( '.tab-content' ) .removeClass ( 'show' );
$ ( '.tab-button' ) .eq ( 0 ) .addClass ( 'orange' );
$ ( '.tab-content' ) .eq ( 0 ) .addClass ( 'show' );
})
$ ( '.tab-button' ) .eq ( 1 ) .on ( 'click' , function (){
$ ( '.tab-button' ) .removeClass ( 'orange' );
$ ( '.tab-content' ) .removeClass ( 'show' );
$ ( '.tab-button' ) .eq ( 1 ) .addClass ( 'orange' );
$ ( '.tab-content' ) .eq ( 1 ) .addClass ( 'show' );
})
$ ( '.tab-button' ) .eq ( 2 ) .on ( 'click' , function (){
$ ( '.tab-button' ) .removeClass ( 'orange' );
$ ( '.tab-content' ) .removeClass ( 'show' );
$ ( '.tab-button' ) .eq ( 2 ) .addClass ( 'orange' );
$ ( '.tab-content' ) .eq ( 2 ) .addClass ( 'show' );
})
๊ทธ๋ฐ๋ฐ ์ด๋ ๊ฒ ์์ฑํ๊ณ ๋ณด๋
$('.tab-button') ์ด ๊ณ์ํด์ ๋ฐ๋ณต ๋๋ค.
์ด๋ด๋ ๋ณ์์ ๋ด์์ ์์ฑํ๋ฉด ์ฑ๋ฅ ์ ์ด์ ์ด ์๋ค.
๐ Ctrl + R ํ๋ฉด ํ์ผ ๋ด์์ ๋ฐ๊พธ๊ธฐ ๊ฐ๋ฅ
๐ Ctrl + Shift + R ํ๋ฉด ํ๋ก์ ํธ ๋ด์์ ๋ฐ๊พธ๊ธฐ ๊ฐ๋ฅ
๐ Ctrl + F ํ๋ฉด ํ์ผ ๋ด์์ ๊ฒ์ ๊ฐ๋ฅ
๐ Ctrl + Shift + F ํ๋ฉด ํ๋ก์ ํธ ๋ด์์ ๊ฒ์ ๊ฐ๋ฅ
Copy var tabBtn = $ ( ".tab-button" );
var tabContent = $ ( ".tab-content" );
tabBtn .eq ( 0 ) .on ( 'click' , function (){
tabBtn .removeClass ( 'orange' );
tabContent .removeClass ( 'show' );
tabBtn .eq ( 0 ) .addClass ( 'orange' );
tabContent .eq ( 0 ) .addClass ( 'show' );
})
tabBtn .eq ( 1 ) .on ( 'click' , function (){
tabBtn .removeClass ( 'orange' );
tabContent .removeClass ( 'show' );
tabBtn .eq ( 1 ) .addClass ( 'orange' );
tabContent .eq ( 1 ) .addClass ( 'show' );
})
tabBtn .eq ( 2 ) .on ( 'click' , function (){
tabBtn .removeClass ( 'orange' );
tabContent .removeClass ( 'show' );
tabBtn .eq ( 2 ) .addClass ( 'orange' );
tabContent .eq ( 2 ) .addClass ( 'show' );
})
์ด๋ ๊ฒ ๋ฐ๊ฟ ์ค ์ ์๋ค.
for ๋ฐ๋ณต๋ฌธ
Copy for ( var i = 0 ; i < 10 ; i ++ ){
console .log (i);
}
for ๋ฐ๋ณต๋ฌธ์ ๊ธฐ๋ณธ ํ์์ ์์ ๊ฐ๋ค
0๋ฒ์งธ ๋ถํฐ ์์ํด์ i๊ฐ 10๋ณด๋ค ์์ผ๋ฉด ์๋ ์ฝ๋๋ฅผ ์คํํ๊ณ i๋ฅผ +1ํด์ค๋ค.
๊ทธ๋ผ ์๊น ์์ฑํ js ์ค 3๋ฒ์ด๋ ๋๊ฐ์ ์ฝ๋๊ฐ ๋ฐ๋ณต๋๋๋ฐ
์ด๊ฑธ ๋ฐ๋ณต๋ฌธ์ ์ฐ๋ฉด ๋ ์ค์ผ ์ ์์ ๊ฒ ๊ฐ๋ค.
Copy for ( let i = 0 ; i < 3 ; i ++ ){
tabBtn .eq (i) .on ( 'click' , function (){
tabBtn .removeClass ( 'orange' );
tabContent .removeClass ( 'show' );
tabBtn .eq (i) .addClass ( 'orange' );
tabContent .eq (i) .addClass ( 'show' );
})
}
์์ ๋ฐ๋ณต๋ฌธ์์ let์ ์ฐ๋ ์ด์ ?
์ง๋๋ฒ ๋ณ์ ์ ์ธ ๋์ var let const๋ฅผ ์์๊ฐํด๋ณด๋ฉด ์ ์ ์๋ค.
for ๋ฐ๋ณต๋ฌธ์์ var๋ ์๋๊ณ let์ ๊ฐ๋ฅํ ์ด์ ?
์์ ๋ฐ๋ณต๋ฌธ์์ var๋ก ํ๋ฉด ์ ๋๋ก ๋์์ ํ์ง์๊ณ let์ผ๋ก ํ๋ฉด ๋์ํ๋ค
์๊ทธ๋ด๊น????
์ปดํจํฐ๊ฐ ์ฝ๋๋ฅผ ์ฝ๋ ์์๋ฅผ ํ๋ฒ ๋ฐ๋ผ๊ฐ๋ณด๋ฉด ์ดํด๊ฐ ์ฝ๋ค.
์ปดํจํฐ๋ ์ฝ๋๋ฅผ ์์์ ๋ถํฐ ์ฝ๋๋ค.
for ๋ฐ๋ณต๋ฌธ์ ๋ฐ๊ฒฌํ๊ณ ๋ฐ๋ณต์คํํ๋ค.
์ด๋ฒคํธ ๋ฆฌ์ค๋๋ฅผ ๋ง๋๊ณ ์ด๋ฒคํธ ๋ฆฌ์ค๋๋ ๋ฐ๋ก ์คํํ๋ ๊ฒ ์๋๋ผ ํด๋น ๋ฒํผ์ ํด๋ฆญํ ๋ ์คํ๋๋ค.
๊ทธ๋์ ์ด๋ฒคํธ๋ฆฌ์ค๋ ๋ด์ ์ฝ๋๋ ์คํํ์ง ์๊ณ ์ง๋๊ฐ๋ค.
๊ทธ๋ ๊ฒ๋๋ฉด ๋ฐ๋ณต๋ฌธ์ ๋๋์ var i = 3์ด๋๋ค.
๋ฐ๋ณต๋ฌธ์ด ๋ค ์ข
๋ฃ๋๊ณ ์ดํ์ ์ฌ์ฉ์๊ฐ ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ด์ ์์ผ ์ด๋ฒคํธ๋ฆฌ์ค๋ ์์ ์ฝ๋๋ฅผ ์คํํ๋ค.
๋ฐ๋ณต๋ฌธ์ ์คํํ๋ ค๊ณ ๋ณด๋ ํ์ฌ var i = 3์ด๋ค??
Copy tabBtn .eq ( 3 ) .on ( 'click' , function (){
tabBtn .removeClass ( 'orange' );
tabContent .removeClass ( 'show' );
tabBtn .eq ( 3 ) .addClass ( 'orange' );
tabContent .eq ( 3 ) .addClass ( 'show' );
})
์ด๋ ๊ฒ ์ฝ๋๊ฐ ๋๋๋ฐ 3๋ฒ์งธ ๋ฒํผ์ ์๋ค.
๊ทธ๋์ var๋ ์ค๋ฅ๋๊ณ let์ ๋ค์ ๋ฐ๋ณต๋ฌธ๋๋ 0๋ถํฐ ์์ํ๊ธฐ๋๋ฌธ์ ์ ์๋์ํ๋ค.
์ง๊ธ๊น์ง ๋ฌธ๋ฒ์ ๋์ดํ๊ณ ๋ค์๊ฐ์ ๋ญ๋ฑ๊ทธ๋ ค์ ํ๋ฒ์ ์ด๊ฒ์ ๊ฒ ๋ค ํด๋ณด๋ ๋ฐฉ์์ด ์๋๋ผ
ํ๋์ฉ ๋ฌด์ธ๊ฐ ๋ง๋ค์ด๋ณด๋ฉด์ ๊ฑฐ๊ธฐ์ ๋ฌธ๋ฒ์ ๋
น์ฌ๋ด๋ ๋ฐฉ์์ด๋ค.
๋ค๋ฅธ ๋ฌธ๋ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์ด๋ฐ ๋ฌธ๋ฒ์ด ์ด๋ ๊ฒ ํ ๋ ์ฐ์ด๋ค
๋ ๋ค๋ฅธ๋ฐ๋ ์ด๋ป๊ฒ ์ฐ์ผ๊น๋ฅผ ๊ณ ๋ฏผํด๋ณด๋ฉด ์ข๋ค.