[Day1] JS 1 [11/18]
[์๋ฐ์คํฌ๋ฆฝํธ(javascript)]
1. ๊ฐ์ฒด ๊ธฐ๋ฐ์ ์น ํ๋ก๊ทธ๋๋ฐ ์ธ์ด ( OOP )
2. ๋ชฉ์ : ์น ํ์ด์ง์ ์ฌ์ฉ์์ ๋ฐ์ ์ฒ๋ฆฌ.
์ฌ์ฉ์์ ํน์ ์ด๋ฒคํธ๋ ์ ๋ ฅ๊ฐ์ ๋ฐ์์ ๋์ ์ผ๋ก ์ฒ๋ฆฌํ ๋ชฉ์ ์ผ๋ก
๊ณ ์๋ ์ธ์ด.
3. html5 - ์น ํ์ด์ง ๊ตฌ์กฐ ์ ์
css3 - ์น ํ์ด์ง์ ์คํ์ผ ์ ์
js - ์น ํ์ด์ง์ ๋์ ์ฒ๋ฆฌ ์ ์ + jquery
ใฑ. html ๋ด์ฉ ๋ณ๊ฒฝ days01.ex01_02.html
- click ํ๋ฉด h ๋ด์ฉ ๋ฐ๋๊ฒ
<body onload="init();" onunload="">
<h3>ex01_02.html</h3>
๋ฉ์์ง ์
๋ ฅ :
<!-- autofocus="autofocus" ์๋ํฌ์ปค์ค -->
<!-- value="์๋
~~~" -->
<input type="text" id="txtMsg" name="txtMsg" >
<br>
<button onclick="clickme();">Click Me!</button>
<p id="demo"></p>
<script>
var txtbox = document.getElementById("txtMsg");
function init(){
// alert( "init()..."); ์ด๊ธฐํ..
// js ๋ก ํฌ์ปค์ค ์ค์
// js focus() ํจ์
/*
document.getElementById("txtMsg").focus();
document.getElementById("txtMsg").value = "Hello World~";
*/
// var txtbox = document.getElementById("txtMsg");
txtbox.focus();
txtbox.value = "Hello World!!!";
}
// js, jquery ๋ฑ๋ฑ ์๋ ์์ฑ...
function clickme(){
// var msg = document.getElementById("txtMsg").value;
var msg = txtbox.value;
// console.log( msg );
// [DOM] ๋ฉ์๋
// document.getElementById(id);
// document.getElementsByClassName(name)
// document.getElementsByName(name)
var h3list = document.getElementsByTagName("h3"); // nodelist
// h3.value = msg; <h3> value ์์ฑ X
h3list[0].innerText = msg;
}
</script>
hover๋ฅผ js, jquery๋ก ๊ตฌํ
<script>
/*
var pdemo = document.querySelector("#demo");
// <์์ํ๊ทธ>innerText</์ข
๋ฃํ๊ทธ>
//pdemo.innerText = "<span style='color:red;background-color:yellow;'>message</span>";
pdemo.innerHTML = "<span style='color:red;background-color:yellow;'>message</span>";
*/
</script>
<script>
/*
var pdemo = document.querySelector("#demo");
pdemo.onmouseover = function (){
// alert("dddd")
// [1] ๋ฐฉ๋ฒ
//this.style.color="red";
//this.style.fontSize="35px";
//this.style.border="1px solid gray";
// [2] ๋ฐฉ๋ฒ
// this.className = "phoverstyle";
// this.classList.add("phoverstyle"); // remove(), replace()
// [3] ๋ฐฉ๋ฒ
// this.setAttribute("class", "phoverstyle"); // getAttribute("class")
}
*/
</script>
<!-- jquery -->
<script>
// innerText == text() jquery method
//$("#demo").text("<span style='color:red;background-color:yellow;'>message</span>");
// innerHTML == html() jquery method
//$("#demo").html("<span style='color:red;background-color:yellow;'>message</span>");
</script>
<script>
/*
$("#demo").mouseover(function() {
//$(this).css("color","red");
//$(this).css("font-size","35px");
//$(this).css("border","1px solid gray");
//$(this)
// .css("color","red")
// .css("font-size","35px")
// .css("border","1px solid gray");
// $(this).css("" , function (){})
// jquery.com
}) ;
*/
// on() jquery method ์ด๋ฒคํธ ๋ฑ๋ก [ + ajax ์ฌ์ฉ ]
$("#demo").on("mouseover", function() {
/*
$(this).css({
"์์ฑ๋ช
":"์์ฑ๊ฐ",
"์์ฑ๋ช
":"์์ฑ๊ฐ",
"์์ฑ๋ช
":"์์ฑ๊ฐ",
"์์ฑ๋ช
":"์์ฑ๊ฐ",
:
});
*/
$(this).css({
"color":"red",
"font-size":"35px",
"border":"1px solid gray"
});
});
</script>
ใด. html ์์ฑ ๋ณ๊ฒฝ ex01_03.html
- Turn on ๋ฒํผ์ ๋๋ฅด๋ฉด ์ ๊ตฌ์ ๋ถ์ ์ผ๋๋ก ํ ์์
- img ์์์ src ์์ฑ์ ../images/pic_bulbon.gif ๋ณ๊ฒฝ
alt ์์ฑ ="bulbon"
<!-- ๋ฌธ์ 2 ํ์ด -->
<button>Turn on</button>
<button disabled="disabled">Turn off</button>
<br>
<img src="../images/pic_bulboff.gif" alt="" id="bulb">
<!-- js -->
<script>
/*
// Turn on ๋ฒํผ
// document.querySelector("button:first-of-type")
document.querySelector("button:nth-of-type(1)").onclick = function() {
// alert( "aaa") ์ด๋ฒคํธ ํ์ธ
// js ๋ณ์ ์ ์ธ ํค์๋ : var, let
let bulb = document.getElementById("bulb");
bulb.src = "../images/pic_bulbon.gif";
// bulb.alt = "bulbon";
bulb.setAttribute("alt", "bulbon");
// turn on ๋ฒํผ ๋นํ์ฑํ
document.querySelector("button:first-of-type").disabled = true;
// turn off ๋ฒํผ ํ์ฑํ
document.querySelector("button:last-of-type").disabled = false;
}
// Turn off ๋ฒํผ
document.querySelector("button:last-of-type").onclick = function() {
//console.log( "bbb"); ์ด๋ฒคํธ ํ์ธ
let bulb = document.getElementById("bulb");
bulb.src = "../images/pic_bulboff.gif";
bulb.setAttribute("alt", "bulboff");
// turn on ๋ฒํผ ํ์ฑํ
document.querySelector("button:first-of-type").disabled = false;
// turn off ๋ฒํผ ๋นํ์ฑํ
document.querySelector("button:last-of-type").disabled = true;
}
*/
</script>
<!-- jquery -->
<script>
// Turn on ๋ฒํผ
$("button:nth-of-type(1)").click(function() {
// jquery method : attr()
/*
$("#bulb")
.attr("src", "../images/pic_bulbon.gif")
.attr("alt", "bulbon");
*/
$("#bulb")
.attr({
src: "../images/pic_bulbon.gif",
alt:"bulbon"
});
// attr() / prop() / is(:checked)
//$(this).prop("disabled", true);
// next() ๋ค์์์
//$(this).next().prop("disabled", false);
$(this)
.prop("disabled", true)
.next()
.prop("disabled", false);
})
// Turn off ๋ฒํผ
$("button:nth-of-type(2)").click(function() {
$("#bulb")
.attr({
src: "../images/pic_bulboff.gif",
alt:"bulboff"
});
$(this)
.prop("disabled", true)
.prev()
.prop("disabled", false);
})
</script>
<h3>ex01_04.html - [ js ]html ์์ฑ ๋ณ๊ฒฝ</h3>
<pre>
[Turn on] ํด๋ฆญํ๋ฉด [Turn off] ์ ๊ตฌ ๋ถ ON
[Turn off] ํด๋ฆญํ๋ฉด [Turn on] ์ ๊ตฌ ๋ถ OFF
</pre>
<!-- ๋ฌธ์ 2 ํ์ด -->
<button>Turn on</button>
<br>
<img src="../images/pic_bulboff.gif" alt="" id="bulb">
<!-- js -->
<script>
/*
document.querySelector("button").onclick = function (){
// alert( this.innerText ); alert( this.innerHTML );
let bulb = document.getElementById("bulb");
var text = this.innerText;
if( text == "Turn on"){ // js if~else equals()
bulb.src = "../images/pic_bulbon.gif";
this.innerText = "Turn off";
}else{
bulb.src = "../images/pic_bulboff.gif";
this.innerText = "Turn on";
} // if
}
*/
</script>
<script>
/*
// ์ค์์น ๋ณ์ ์ฌ์ฉ
var sw = false;
let bulb = document.getElementById("bulb");
document.querySelector("button").onclick = function (){
// if( !sw ){
// bulb.src = "../images/pic_bulbon.gif";
// }else{
// bulb.src = "../images/pic_bulboff.gif";
// } // if
// js ์ผํญ์ฐ์ฐ์ ? :
// bulb.src = sw ? "../images/pic_bulboff.gif" : "../images/pic_bulbon.gif";
bulb.src = "../images/pic_bulb" + ( sw ? "off" : "on" ) + ".gif";
this.innerText = sw ? "Turn on" : "Turn off";
sw = !sw;
}
*/
</script>
ใท. html ์คํ์ผ(css) ๋ณ๊ฒฝ
ใน. html ์์ ์จ๊ธฐ๊ธฐ/๋ณด์ด๊ธฐ
๋ฑ๋ฑ
4. ์ฌ์ฉ์ ๊ฒฝํ ( UX ) / UI ์ ํฅ์์ํค๊ธฐ ์ํ ๋ฐฉ๋ฒ : js ์ฃผ๋ก ์ฌ์ฉ.
5. js = ์น๋ธ๋ผ์ฐ์ (ํด๋ผ์ด์ธํธ) ์ธ์ด + ์ง๊ธ์ ์๋น ์ธก ํ๋ก๊ทธ๋จ ๊ฐ๋ฐ ์ฌ์ฉ( ํ๋ )
node.js
6. js ๋ค์ด๋ก๋ X / ์ค์น X => ๋ธ๋ผ์ฐ์ ๋ด์ฅ( js ์์ง )
๋ธ๋ผ์ฐ์ ์ข ๋ฅ / ๋ฒ์ js X
7. ํ์ค js ๋ ECMAScript ( ์ํฌ๋ง ์คํฌ๋ฆฝํธ)
ํ์ค ๊ณต์ ์ด๋ฆ : ECMA-262
8. js๋ <script></script> (๋ฐ๋, ํค๋ ์์ ๊ถ์ฅ)
9. <script></script> ํ๊ทธ๋ ์ํ๋ ์์น์ ์ํ๋ ์๋งํผ ์ฌ์ฉํ ์ ์๋ค.
ex03.html
10. ๋์ผํ js ์ฝ๋ฉ์ด ์ฌ๋ฌ ํ์ด์ง์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ๋๋ค๋ฉด
์ธ๋ถ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ( ??????.js ) ์ฌ์ฉ.
<script src="ex01_myscript.js"></script>
/** * ex01_myscript.js */ function myAlert( name ){ alert( name +"๋ ํ์~~" ); } |
- ์ธ๋ถ js ํ์ผ ์ฌ์ฉ ์ฅ์
1) html ์ฝ๋ฉ + js ์ฝ๋ฉ ๋ถ๋ฆฌ -> ๋ ์ฝ๊ฒ ์ฝ๊ณ ์ ์ง ๋ณด์ ๊ฐ๋ฅ
2) ์บ์ฑ๋ js ํ์ผ์ ํ์ด์ง๋ฅผ ๋ก๋ฉ ์๋ ๋ ํฅ์.
- ์ธ๋ถ js ํ์ผ 3๊ฐ์ง ๋ฐฉ๋ฒ
1) ์ ์ฒด URL
https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js
2) ํ์ผ ๊ฒฝ๋ก
../js/myscript.js
3) ๊ฐ์ ๊ฒฝ๋ก
myscript.js
11. body ์์ ๋งจ ์๋์ scirpt ๋ฅผ ๋ฐฐ์นํ๋ฉด ํ๋ฉด ์ถ๋ ฅํ๋ ์๋๊ฐ ํฅ์๋๋ค.
*autofocus ์์์๋ body onload
* getElements VS element
๋ฌธ์ 4๋ฒ ํ์ด : ๋ผ๋์ค์ฒดํฌ
<fieldset>
<legend>html ์ ์ฉ์ฌ๋ถ</legend>
<input type="radio" name="htmlAppy" checked="checked" value="apply"><label>์ ์ฉ</label>
<input type="radio" name="htmlAppy" value="not apply"><label>๋น์ ์ฉ</label>
</fieldset>
<input type="text" value="<b>ํ๊ธธ๋</b>">
<br>
<button>ํ์ธ</button>
<p id="demo"></p>
<!-- js -->
<script>
/*
document.querySelector("button").onclick = function (){
// document.getElementsByName("htmlAppy");
var rdbs = document.querySelectorAll("fieldset > input[type=radio]");
// alert( rdbs.length );
// ๋ผ๋์ค๋ฒํผ 1๊ฐ๋ง ์ฒดํฌ.
var txtValue = document.querySelector("input[type=text]").value;
if ( rdbs[0].checked ) { // ์ ์ฉ
document.querySelector("#demo").innerHTML = txtValue ;
} else { // ๋น์ ์ฉ
document.querySelector("#demo").innerText = txtValue;
}
}
*/
</script>
<!-- jquery -->
<script>
$("button").click(function (){
var rdbs = $("fieldset > input[type=radio]");
var txtValue = $("input[type=text]").val();
if ( rdbs[0].checked ) { // ์ ์ฉ attr()/prop()/is()
$("#demo").html( txtValue );
} else { // ๋น์ ์ฉ
$("#demo").text( txtValue );
}
});
</script>
[js ๋ฐ์ดํฐ ์ถ๋ ฅ ๊ธฐ๋ฅ]
1. document.write();
2. window.alert("") ; - ๊ฒฝ๊ณ ์ฐฝ์ผ๋ก ๋ฐ์ดํฐ ์ถ๋ ฅ
alert("");
3. console.log() - ๋ธ๋ผ์ฐ์ ์ฝ์ ์ฐฝ์ ๋ฐ์ดํฐ ์ถ๋ ฅ( ๋๋ฒ๊น ๋ชฉ์ )
4. ์์.innerText ์์ฑ -> text()
์์.innerHTML ์์ฑ -> html()
๋ฌธ์ 6
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<style>
.active{
background-color: yellow;
color:rgb(206, 233, 96);
font-size: 25px;
}
</style>
</head>
<body>
์ข์ํ๋ ๊ณผ๋ชฉ ์ ํ?
<br>
<input type="checkbox" value="kor" name="subject" id="s_kor" ><label for="s_kor">๊ตญ์ด</label>
<input type="checkbox" value="eng" name="subject" id="s_eng" checked="checked"><label for="s_eng">์์ด</label>
<input type="checkbox" value="mat" name="subject" id="s_mat" ><label for="s_mat">์ํ</label>
<input type="checkbox" value="pe" name="subject" id="s_pe" ><label for="s_pe">์ฒด์ก</label>
<br>
<br>
<button onclick="">ํ์ธ</button>
<p id="demo"></p>
<!-- js -->
<script>
/*
document.querySelector("button").onclick = function (){
document.getElementById("demo").innerHTML = "";
// alert("์ด๋ฒคํธ ํ์ธ")
//var ckbs = document.querySelectorAll("input[type=checkbox][name=subject]");
//var ckbs = document.querySelectorAll("[name=subject]");
var ckbs = document.getElementsByName("subject");
// alert( ckbs.length );
var content = "";
for (var i = 0; i < ckbs.length; i++) {
if( ckbs[i].checked ){
// console.log( ckbs[i].value );
//document.getElementById("demo").innerHTML += ckbs[i].value+"<br>";
if( ckbs[i].value == "kor" ){
content += "<b class='active'>"+ ckbs[i].value+"</b><br>";
}else{
content += "<b>"+ ckbs[i].value+"</b><br>";
}
}
} // for
document.getElementById("demo").innerHTML = content;
}
*/
</script>
<!-- jquery -->
<script>
/*
$("button").click( function (){
$("#demo").html("");
// $("[name=subject]")
// jquery selector - :checkbox
// $(":checkbox") ==$("input[type=checkbox]")
var ckbs = $(":checkbox[name=subject]");
// alert( ckbs.length );
for (var i = 0; i < ckbs.length; i++) {
//if ( elem.checked ) - js
//if ( $( elem ).prop( "checked" ) )
//if ( $( elem ).is( ":checked" ) )
// Uncaught TypeError: ckbs[i].prop is not a function
// jquery object = jquery method : prop(), val(), ....
// *** $(html element) -> jquery object ๋ณํ ***
if( $(ckbs[i]).prop( "checked" ) ){
// Uncaught TypeError: ckbs[i].val is not a function
// console.log( $(ckbs[i]).val() ); // val() ๋ฉ์๋ == value ์์ฑ
// $("#demo").html( $("#demo").html() + $(ckbs[i]).val() +"<br>" );
$("#demo").html( function (index, oldhtml){
return oldhtml +"<br>" + $(ckbs[i]).val();
} );
} // if
} //for
} );
*/
</script>
<!-- jquery ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ -->
<script>
$("button").click( function (){
$("#demo").html("");
// ์ฒดํฌ๋ฐ์ค ์ค์ ์ฒดํฌ๋ ์ฒดํฌ ๋ฐ์ค๋ฅผ ์ป์ด์ค๋ ๊ฒ.
/* jquery selector - :checked */
// ๋ฐ๋ณต๋ฌธ - value ์ป์ด์์ -> #demo ์ถ๋ ฅ
// jquery method - each()
$("[name=subject]:checked").each(function (index, element){
// element.val()
// console.log( index +" / " + element.value + " / " + $(element).val() +" / " + $(this).val() );
$("#demo").html( function (index, oldhtml){
return oldhtml +"<br>" + $(element).next().text();
});
});
} );
</script>
* jquery ๊ฐ์ฒด๋ง .prop ์ฌ์ฉ๊ฐ๋ฅ (๋ณํ์์ผ์ผํจ)
1. option์ ์ ํํ๊ฒ๋๋ฉด ์ผ์ด๋๋ ์ด๋ฒคํธ - [ change ์ด๋ฒคํธ ]
2. ์ฌ๋ฌ option ๋ค ์ค์ ์ด๋ค option์ด ์ ํ
- selectedIndex ์์ฑ
3. ์ ํ๋ option์ value, text
select.options[selectedIndex].value, .text ์ปฌ๋ ์
์ฃผ์) ์ ํ๋ ์ต์ ์ ๋ค์ ์ ํํ๋ฉด chage ์ด๋ฒคํธ๋ ๋ฐ์ํ์ง ์๋๋ค.
<br>
<!-- select>option*5 -->
<!-- select ์์์์ text ์์ฑ๋ง ์ค์ ํ๋ value ์์ฑ์ ๋์ผํ ๊ฐ. -->
<select name="subject" id="subject">
<option value="">์ ํํ์ธ์.</option>
<option value="kor">๊ตญ์ด</option>
<option value="eng">์์ด</option>
<option value="mat">์ํ</option>
<option value="pe">์ฒด์ก</option>
</select>
<br>
<p id="demo"></p>
<!-- js -->
<script>
/*
document.getElementById("subject").onchange= function (){
// console.log( "change event ~");
// console.log( this.selectedIndex );
var index = this.selectedIndex;
var op = this.options[index];
if( op.value == 0 ) {
alert("์ ํํ์ธ์~~")
}else{
console.log( op.value + " / " + op.text );
}
}
*/
</script>
<script>
/*
// kor - undefined
document.getElementById("subject").onchange= function (){
console.log( this.value + " - " + this.text );
}
*/
</script>
<!-- jquery -->
<script>
// alert( $("#subject option").length );
// alert( $("#subject option").size() ); X
$("#subject").change(function() {
// console.log( $(this).val() +" // " + $(this).text() );
// jquery selector - :checked, :selected
// console.log( $("#subject option:selected").text() );
// jquery method - find()
console.log( $(this).val() +" // " + $(this).find(":selected").text() );
// http://erel.kr/bbs/board.php?bo_table=study&wr_id=275
});
</script>
1. js ์ฝ๋ฉ์ ๋๊ฐ ์คํ ? ์น ๋ธ๋ผ์ฐ์ (*** ํด๋ผ์ด์ธํธ ***))๊ฐ ์คํ.
2. ์์ฑ๋ ์์๋๋ก ํ๋์ฉ ์คํ๋๋ค.
3. ์คํ๋ฌธ(๋ช ๋ น๋ฌธ) ๋ค์ ์ธ๋ฏธ์ฝ๋ก ์ ์ถ๊ฐ.
js ๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ฐ์ด๋ ๋๋ค.
ํ์ง๋ง ์ธ๋ฏธ์ฝ๋ก ์ ์ถ๊ฐํ๋ ๊ฒ์ ( ์ ๊ทน๊ถ์ฅ )ํ๋ค.
4. js ๋ ์ฌ๋ฌ ๊ณต๋ฐฑ์ ๋ฌด์ํ๋ค.
๊ณต๋ฐฑ์ ์ถ๊ฐํ๋ฉด ๊ฐ๋ ์ฑ์ด ์ข๋ค.
var name = xx;
var sum = m + l + x;
5. js ์์ฝ์ด(ํค์๋)
var
let
const ํค์๋ == ์์ ์ ์ธ. ( final double PI = 3.14)
if
for
function
abstract arguments await* boolean
break byte case catch
char class* const continue
debugger default delete do
double else enum* eval
export* extends* false final
finally float for function
goto if implements import*
in instanceof int interface
let* long native new
null package private protected
public return short static
super* switch synchronized this
throw throws transient true
try typeof var void
volatile while with yield
6. ๋ฆฌํฐ๋ด( ๊ณ ์ ๊ฐ )
3.14 'A' [ "admin" 'admin' ] true false ๋ฑ๋ฑ
7. js ๋ ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ค.
8. js์ ๋ณ์ ์ ์ธ ํ์ : firstNameName
getFirstName
์บ๋ฉํ๊ธฐ๋ฒ
9. js ์๋ณ์ ๋ช ๋ช ํ๋ ๊ท์น
10. js ์ฃผ์์ฒ๋ฆฌ // /*~ */
/*
var name = 'admin' ;
var age = 10;
Age = 20;
*/
//var name = "John Doe";
//var name = 'John Doe';
var name = "John's Doe";
// js๋ ๋ณ์๋ฅผ ์ ์ธํ์ง ์๊ณ ์ฌ์ฉํ ์ ์๋ค.
Name = "admin";
alert( name + " / " + Name );
[ js ๋ณ์๋ฅผ ์ ์ธํ๋ 4๊ฐ์ง ๋ฐฉ๋ฒ ]
1. var : 1995๋ ~ 2015๋ ๊น์ง js ์ฝ๋์์ ์ฌ์ฉ.
2. let : 2015๋ ( ES6 ) js ์ถ๊ฐ๋ ํค์๋
3. const : ์์ํ( ๋ณ์๊ฐ์ ๋ณ๊ฒฝ X )
4. ์๋ฌด๊ฒ๋ ์ฌ์ฉํ์ง ์๋ ๊ฒฝ์ฐ
์ต๊ทผ๋๊ธ