css
* { margin: 0; padding: 0; }
article { width: 60%; margin: 80px auto; }
h2 { padding: 10px 0; }
input { height: 30px; border: 1px solid #ccc; background: #f7f7f7 ; }
input:focus {
outline: none;
border: 1px solid #bbe2f1;
background: #daeff7;
}
button {
height: 32px;
border: 0;
padding: 0px 20px;
background: powderblue;
cursor: pointer;
vertical-align: bottom;
}
a { text-decoration: none; color: #111; }
li { list-style: none; padding: 10px; }
.form1 li {
border-bottom: 1px solid #ccc;
}
html
<article class="form1">
<h2>์ถ์์ฒดํฌ</h2>
<div>
<input type="text" />
<button>์ ์ฅ</button>
</div>
<ul></ul>
</article>
js
const inputForm1 = document.querySelector(".form1 input")
const btnForm1 = document.querySelector(".form1 button")
const ulForm1 = document.querySelector(".form1 ul")
btnForm1.onclick = function(){
if (inputForm1.value != "") { // ์
๋ ฅํ ๋ด์ฉ์ด ์์ ๋
const createLi = document.createElement("li"); // liํ๊ทธ ์์ฑ
createLi.append(inputForm1.value); // ์์ฑ๋ li์ input ์
๋ ฅ๊ฐ์ ์ฝ์
(๋งจ ๋ค)
// value - ๊ฐ
ulForm1.prepend(createLi); // ์์์ ๋ง๋ li๋ฅผ ul์ ์ฝ์
(๋งจ ์)
console.log(ulForm1);
inputForm1.value = ""; // ์ ์ฅ ๋ฒํผ์ ๋๋ฅด๋ฉด input์ ๋น์ (reset)
inputForm1.focus(); // input์ focus๋ฅผ ๋ง์ถค
}
};
css
body {
font-family: Verdana, Geneva, Tahoma, sans-serif;
background: #d3d3d3;
}
/* header */
header { background: #fff; }
header div {
max-width: 1000px;
margin: auto;
height: 80px;
}
header h1 {
font-size: 3em; /* 16px * 3 */
line-height: 80px;
}
/* main */
main { background: #818181; }
main > div { padding: 50px; text-align: center; }
main img { max-width: 1000px; }
button { }
.popup {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, .7);
text-align: center;
}
.popup > div {
display: inline-block;
padding: 30px;
background: #fff;
margin-top: 100px;
}
.popup img { height: 60vh; }
/* footer */
footer { padding: 30px 0; }
footer div {
max-width: 1000px;
margin: auto;
text-align: right;
}
footer small {
color: #fff;
font-size: 0.8em;
}
html
<header>
<div>
<h1>jQuery - attr(์์ฑ๋ณ๊ฒฝ)</h1>
</div>
</header>
<main>
<div>
<img src="./img/cat2.jpg" alt="๊ณ ์์ด" title="๊ณ ์์ด"> <br><br>
<button class="btn1">๊ณ ์์ด</button>
<button class="btn2">๊ฐ์์ง</button>
<button class="btn3">์ ์ฐฝ ์ด๊ธฐ</button>
</div>
</main>
<div class="popup">
<div>
<img src="./img/dog2.png" alt=""><br><br>
<button>CLOSE X</button>
</div>
</div>
<footer>
<div><small>jQuery ์ฐ์ต ํ์ด์ง ์
๋๋ค.</small></div>
</footer>
jQuery
var elImg = $("main img").attr("src"); // ์ด๋ฏธ์ง์ src์ value
console.log(elImg);
$(".btn1").click(function(){
$("main img").attr({ "src": "img/cat2.jpg" , "alt": "๊ณ ์์ด", "title": "" }); // title key๊ฐ๋ง ๋จ์
$("main img").removeAttr("title"); // title ์ ์ฒด๊ฐ ์ฌ๋ผ์ง
});
$(".btn2").click(function(){
$("main img").attr({ "src": "img/dog1.jpg" , "alt": "๊ฐ์์ง", "title": "ํ
์คํธ" });
});
$(".btn3").click(function(){
$(".popup").fadeIn(1000);
})
$(".popup button").click(function(){
$(".popup").fadeOut(200);
})
/*
A.attr(์์ฑ๋ช
) - ์๋ฆฌ๋จผํธ A์์์ ์์ฑ๊ฐ์ ๊ฐ์ ธ์ด
A.attr({ ์์ฑ๋ช
(key): ์์ฑ๊ฐ(value) }) - A์์์ ์์ฑ์ ์ ์ด/์ถ๊ฐ
A.removeAttr(์์ฑ๋ช
) - A์์์ ์๋ ์์ฑ์ ์ ๊ฑฐ
*/
css
body { font-family:Verdana, Geneva, Tahoma, sans-serif; background:#d3d3d3;}
header { background:#fff;}
header div { max-width:1000px; margin:auto; height:80px;}
header h1 { font-size:3em; line-height:80px;}
main { background:#929090; padding:100px; }
main ul::after { display: block; content: ""; clear:both; }
main li { float:left; width:300px; height:300px; margin-bottom:30px; }
main li button { padding:5px 10px; border:none; border-radius:5px; margin-bottom:10px; cursor:pointer;}
.box { display:flex; justify-content:center; align-items:center; width:250px; height:250px; background:lavenderblush; }
.red { background: red; }
.orange { background:orange; }
.bd { border: 5px solid black; }
footer { padding:30px 0; }
footer div { max-width:1000px; margin:auto; text-align:right; }
footer small { color:#fff; font-size:0.8em; }
html
<header>
<div><h1>jQuery - class ๋ฉ์๋</h1></div>
</header>
<main>
<ul>
<li class="method1">
<button>addClass</button>
<button>removeClass</button>
<button>All</button>
<div class="box">addClass / removeClass</div>
</li>
<li class="method2">
<button>toggleClass</button>
<div class="box">toggleClass</div>
</li>
<li class="method3">
<button>orange</button>
<button>border</button>
<button>oragne+border</button>
<button>reset</button>
<div class="box"></div>
โ
โ
โ
โ
โ
โ
โ
โ
โ
โ
</li>
</ul>
</main>
<footer>
<div><small>jQuery์ฐ์ต ํ์ด์ง์
๋๋ค</small></div>
</footer>
jQuery
$(function () {
$(".method1 button:nth-child(1)").click(function () {
$(".method1 .box").addClass("red");
});
$(".method1 button:nth-child(2)").click(function () {
$(".method1 .box").removeClass("red");
});
$(".method1 button:nth-child(3)").click(function () {
$(".method1 .box").removeClass();
});
$(".method2 button").click(function () {
$(".method2 .box").toggleClass("red bd");
// ํด๋์ค ์ฌ๋ฌ ๊ฐ ์ ์ฉ ์
});
var $box3 = $(".method3 .box"); // ์ ํ์๋ฅผ ๋ณ์๋ก
$(".method3 button:nth-child(1)").click(function () {
$box3.addClass("orange");
});
$(".method3 button:nth-child(2)").click(function () {
$box3.addClass("bd");
});
$(".method3 button:nth-child(3)").click(function () {
if ($box3.hasClass("orange")) {
$box3.addClass("bd");
} else if ($box3.hasClass("bd")) {
$box3.addClass("orange");
} else {
$box3.html("<p>์ ์ฉ๋ ํด๋์ค๊ฐ ์์</p>");
}
});
$(".method3 button:nth-child(4)").click(function () {
$box3.removeClass("orange bd").html(""); // ๋ฉ์๋ ์ฒด์ธ
});
});
/*
A.addClass(b) - ์๋ฆฌ๋จผํธ A์ ํด๋์ค b ์ถ๊ฐ
A.removeClass(b) - A์ ์ ์ฉ๋์ด์๋ ํด๋์ค b ์ ๊ฑฐ
A.removeClass() - A์ ์ ์ฉ๋์ด ์๋ ๋ชจ๋ ํด๋์ค ์ ๊ฑฐ. ๊ดํธ๊ฐ ๋น์ด์์
A.toggleClass(b) - A์ ํด๋์ค b๋ฅผ ์ถ๊ฐ. ์ฌ๋ฌ ๊ฐ ๊ฐ๋ฅ
*/
๊ธฐ๋ณธ
addClass / removeClass
toggleClass
orange
border
์ ์ฉ๋ ํด๋์ค๊ฐ ์์ ๋
reset
css
body {
font-family: Verdana, Geneva, Tahoma, sans-serif;
background: #d3d3d3;
}
header { background: #fff; }
header div {
max-width: 1000px;
margin: auto;
height: 80px;
}
header h1 {
font-size: 3em; /* 16px * 3 */
line-height: 80px;
}
main { background: #818181; padding: 100px 0; }
.tab {
width: 450px;
margin: auto;
}
.tabMenu { display: flex; }
.tabMenu li {
flex: 1; /* 1:1:1 */
border: 1px solid #505050;
color: #fff;
background: #aaa;
height: 40px;
line-height: 40px;
text-align: center;
border-right: none;
}
.tabMenu li:last-child { border-right: 1px solid #505050; }
.tabMenu li.active {
background: #fff;
color: #000;
}
footer { padding: 30px 0; }
footer div {
max-width: 1000px;
margin: auto;
text-align: right;
}
footer small {
color: #fff;
font-size: 0.8em;
}
html
<header>
<div>
<h1>jQuery - tab</h1>
</div>
</header>
<main>
<div class="tab">
<ul class="tabMenu">
<li class="active">MENU1</li>
<li>MENU2</li>
<li>MENU3</li>
</ul>
</div>
</main>
<footer>
<div><small>jQuery ์ฐ์ต ํ์ด์ง ์
๋๋ค.</small></div>
</footer>
jQuery
$(".tabMenu li").click(function () {
let num = $(this).index();
console.log(num);
$(".tabMenu li").removeClass("active"); // 3๊ฐ์ li ์ ๋ถ ํด๋์ค active ์ ๊ฑฐ
$(this).addClass("active");
});
/*
A.index() - A๊ฐ ๋ช ๋ฒ์งธ ์์์ธ์ง ์์์จ๋ค (0๋ถํฐ ์์)
*/