[Day2] JS 2 - λ³μ, μ°μ°μ, μλ£ν, ν€λ³΄λ
[Day2] JS 2 [11/21]
--볡μ΅--
1. λͺ¨λ μ ν 체ν¬λ°μ€ μ²λ¦¬
<div>
<input type="checkbox">λͺ¨λ μ ν
<br>
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
<input type="checkbox">
</div>
λͺ¨λ μ νμ΄λΌλ μ²΄ν¬ λ°μ€λ₯Ό 체ν¬νλ©΄ λλ¨Έμ§ λͺ¨λ μ²΄ν¬ λ°μ€λ₯Ό 체ν¬νλλ‘ μ½λ©νμΈμ.
-js-
<script>
document.querySelector("div > input[type=checkbox]:first-of-type").onclick = function (){
// aler t("μ΄λ²€νΈ νμΈ")
//alert( this.checked );
// getAttribute()
// :not(selector)
var ckbs = document.querySelectorAll("div > input[type=checkbox]:not( input[type=checkbox]:first-of-type)");
for (var i = 0; i < ckbs.length; i++) {
ckbs[i].checked = this.checked ;
} // for
}
</script>
-jquery-
// $(":checkbox:first-of-type").click(function (){
$(":checkbox").eq(0).click(function (){
// $(":checkbox:not:(first-of-type)")
// filterλ‘ μ²«λ²μ§Έκ° μλ κ²λ€ νμΈ
$(":checkbox").filter(function(index){
return index >=1;
})
.click(function() {
$(this).hide();
});
});
// μ§μ§μ½λ©
$(":checkbox")
.filter(function(index){
return index >= 1;
}).prop("checked", $(":checkbox").eq(0).prop("checked"));
// .prop("checked", $(this).prop("checked"));
});
* $("input[type=checkbox]") = $(":checkbox")
2. p νκΈ λμ μΌλ‘ 10κ° μμ±νκ³ , p νκ·Έλ₯Ό ν΄λ¦ν λ κ·Έ p νκ·Έλ₯Ό μ¨κΈ°λ js μ½λ©μ νμΈμ
-js-
<script>
for (var i = 1; i <= 10 ; i++) {
// DOM μ¬μ© - μμ μ‘°μ( μμ±, μμ , μμ )
// Document Object Model
// λ¬Έμ -> κ°μ²΄ + μμ±, λ©μλ
let pEle = document.createElement("p"); // Node
pEle.innerText = "μμ±λ p - " + i;
pEle.style.border = "1px solid gray";
// pEle ν΄λ¦ μ΄λ²€νΈ λ±λ‘ - addEventListener()/ removeEventListener()
pEle.addEventListener("click", function( ) {
this.style.display = "none";
});
document.body.appendChild(pEle);
} // for
</script>
* DOM : λ¬Έμλ₯Ό κ°μ²΄νμν€λ κ² (μμ± λ©μλλ₯Ό μ¬μ©ν΄μ)
ex) document.createElement("p") -> μμ μμ± DOM λ©μλ
-jquery-
<script>
// jquery method : body.append( p ), p.appendTo(body)
for (var i = 1; i <= 10 ; i++) {
$("<p></p>")
.text( "μμ±λ p - " + i)
.css("border", "1px solid red")
.click( function (){
$(this).hide();
})
.appendTo($("body"));
} // for
</script>
* append μ’ λ₯ λκ°μ§
3. select νκ·Έμμ κ³Όλͺ©μ μ νν΄μ λΌλμ€λ²νΌμ μ νν κ³Όλͺ©μ 체ν¬νλ js μ½λ©μ νμΈμ
<select id="selsubject" >
<option>μ ννμΈμ.</option>
<option value="kor">κ΅μ΄</option>
<option value="eng">μμ΄</option>
<option value="mat">μν</option>
<option value="pe">체μ‘</option>
</select>
<br>
<input type="radio" name="subject" value="kor"><label>κ΅μ΄</label>
<input type="radio" name="subject" value="eng"><label>μμ΄</label>
<input type="radio" name="subject" value="mat"><label>μν</label>
<input type="radio" name="subject" value="pe"><label>체μ‘</label>
-js-
<script>
document.getElementById("selsubject").onchange = function (){
// console.log( "μ΄λ²€νΈ νμΈ~" );
// alert( this.selectedIndex );
// options 컬λ μ
var svalue = this.options[this.selectedIndex].value ;
// alert( this.options[this.selectedIndex].text );
// alert( svalue );
if(svalue == "μ ννμΈμ."){
alert("κ³Όλͺ©λ§ μ ννμΈμ.");
return;
}
var radios = document.getElementsByName("subject");
// alert( radios.length );
for (var i = 0; i < radios.length; i++) {
if( radios[i].value == svalue ){
radios[i].checked = true;
break; // return;
} // if
} // for
}
</script>
-jquery-
<script>
$("#selsubject").change(function() {
// var svalue = $(this).val() ; // "kor"
// $("input[type=radio]")
// jquery selector - :radio
// jquery method - each()
/*
$(":radio").each(function(i, element) {
if( $(element).val() == svalue ) $(element).prop("checked", true);
});
*/
$(":radio[value="+ $(this).val() +"]").prop("checked", true);
});
</script>
3-1. λΌλμ€ λ²νΌ μ ν -> select μ ν
-js-
<script>
/*
// document.getElementById("selsubject").options
// children - DOM λ©μλ
var ops = document.getElementById("selsubject").children;
// alert( ops.length );
// ops[4].setAttribute("selected", "selected");
ops[4].selected = true;
*/
var ops = document.getElementById("selsubject").children;
var radioIndex = 0;
var radios = document.getElementsByName("subject");
// [λ¬Έμ ] index
// μ ? 4
// let ν€μλ / var ν€μλ μ°¨μ΄μ X
//for (var i = 0; i < radios.length; i++) {
for (let i = 0; i < radios.length; i++) {
// if( radios[i].checked ) radioIndex = i;
radios[i].onclick = function (){
// alert( i + " / " + this.value ) ;
ops[i+1].selected = true;
} // click
} // for
// alert( radioIndex )
</script>
-jquery-
<script>
// var opts = $("#selsubject > option");
$(":radio[name=subject]").click(function() {
// jquery method : index()
var idx = $(":radio[name=subject]").index( this );
// alert( idx );
// ex01.html:48 Uncaught TypeError: $(...)[(idx + 1)].prop is not a function
//$("#selsubject > option")[idx+1].prop("selected", true);
$("#selsubject > option").eq(idx+1).prop("selected", true);
});
</script>
5. <img src="../images/pic_bulboff.gif" alt="" id="bulb">
λ§μ°μ€λ₯Ό μ¬λ¦¬λ©΄ λΆμ΄μΌμ§κ³ , λ§μ°μ€λ₯Ό λ΄λ¦¬λ©΄ λΆμ΄ λμ§λλ‘ js μ½λ©νμΈμ.
-js-
var blub = d.g("bulb");
blub.onmouseover = function (){
this.src = "on.gif";
event.srcElement = "on.gif";
}
blub.onmouseout = function (){
this.src = "off.gif";
event.srcElement = "off.gif";
}
-jquery-
$("#bulb").hover(
function (){ // mouseover
attr() / prop()
},
function (){ // mouseout
}
);
λ³μ
[ js λ³μλ₯Ό μ μΈ 4κ°μ§ λ°©λ² ]
1) var i;
2) let i;
3) const i;
4) i=100;
1. λ³μ ? λ°μ΄ν° μ μ₯ 곡κ°
2. μλ³μ(λ³μ) μ μΈ νμ
1) λ¬Έμ, μ«μ, _ , $ ν¬ν¨λ μ μλ€.
2) μ«μλ‘ μμ X
3) var $$$$; X μ ? jQuery(selector) == $(selector)
4) λμλ¬Έμ κ΅¬λΆ νλ€.
5) μμ½μ΄ X var _var;
- var ν€μλ : 2015 κΉμ§ js μ½λ© μ¬μ©
- let ν€μλ + const ν€μλ : 2015( ES6 ) μ΄ν μΆκ°
- var ν€μλλ‘ μ μΈν λ³μλ μ€λ³΅ μ μΈ O + κ° μ μ§
- let ν€μλλ‘ μ μΈν λ³μλ μ€λ³΅ μ μΈ X
- var lastName = 'λ¬Έμμ΄' ;
- z = 1 + "2"; => "12"
z = 1 + 2 + "3" => 3 +"3" => "33"
- let $$ = 200; κ°λ₯νλ€. (jquery λλ¬Έμ $μ¬μ©νμ§λ§μ)
let ν€μλ
1. let λ‘ μ μΈλ λ³μλ μ¬μ μΈν μ μλ€.
2. let λ‘ μ μΈλ λ³μλ μ¬μ©νκΈ° μ μ μ μΈν΄μΌ λλ€.
3. letλ‘ μ μΈλ λ³μλ κΈλ‘λ² λ²μ, ν¨μ λ²μ + λΈλ‘λ²μ
var κΈλ‘λ² λ²μ, ν¨μ λ²μ
// Global Scope( μ μλ³μ )
var x ;
function test(){
// Function Scope ( μ§μλ³μ )
var y;
// Block Scope
{
let z ;
}
}
*νΈμ΄μ€ν : λΈλ 맨 μλ‘ μ μΈλ μ½λ©μ΄ μ¬λΌκ° (var O let X)
const μμ
1. μ¬μ μΈ ν μ μλ€.
2. μ¬ν λΉ ν μ μλ€.
3. λΈλ‘λͺ¨λ( block scope ) O
[ js μμ const ν€μλ μ¬μ© ? ]
νμ λ³κ²½ν μ μλ .....
const array
const object
const function
const RegExp
*typeof
var data; // [ undefined ]
data = 10; // μ μ [ number ]
data = 3.14; // μ€μ [ number ]
data = "admin"; // λ¬Έμμ΄ [ string]
data = true; // [ boolean ]
data = []; // [] == js λ°°μ΄ [ object]
data = {}; // {} == js κ°μ²΄(Object) [ object]
data = function (){}; // js ν¨μ [ function ]
data = 'a'; // js char X [ string ]
// typeof μ°μ°μ
alert( typeof ( data ));
[ js μ°μ°μ ]
1. μ°μ μ°μ°μ.
2. ν λΉ(λμ ) μ°μ°μ.
3. λΉκ΅ μ°μ°μ
4. λ Όλ¦¬ μ°μ°μ
5. 쑰건 μ°μ°μ ?:
6. μ ν μ°μ°μ
<script>
// μ¦κ°μ°μ°μ ++ --
// λ¬Έμμ΄ μ°κ²°μ°μ°μ + "νκΈΈλ" + "λ"
</script>
<script>
// type(μ ν) μ°μ°μ
// typeof (10) number
// instanceof μ°μ°μ
</script>
<script>
// && || !
/*
console.log( true && true);
console.log( true || true);
console.log( !true );
*/
// js λΉνΈ μ°μ°μ : & | ^ ~ , << >> >>>
</script>
<script>
/*
console.log( 3 == 5 );
console.log( 3 === 5 ); // euqal value + type νμ
κΉμ§ λΉκ΅νλ μ°μ°μ.
console.log( 3 != 5 );
console.log( 3 !== 5 ); // not equal value + type
console.log( 3 > 5 );
console.log( 3 < 5 );
console.log( 3 >= 5 );
console.log( 3 <= 5 );
*/
// ===
// !==
// ? : μΌν(쑰건) μ°μ°μ
</script>
<script>
/*
var x = 5; // = ν λΉ μ°μ°μ
x += 5; // += λ³΅ν© λμ
μ°μ°μ
x -= 5;
x *= 5;
x /= 5;
x %= 5;
x **= 5; λμΉ(power)
*/
</script>
<script>
/*
console.log( 3 + 5 ); // λ§μ
μ°μ°μ 8
console.log( 3 - 5 ); // λΊμ
μ°μ°μ -2
console.log( 3 * 5 ); // κ³±μ
μ°μ°μ 15
console.log( 3 ** 5 ); // Math.pow(3,5) λμΉ μ°μ°μ
// console.log( 3 / 0 ); // λλμ
μ°μ°μ 0.6
// μ μ / 0 Infinity(무νλ)
console.log( 3.1 / 0 ); // λλμ
μ°μ°μ 0.6
// μ€μ / 0 Infinity(무νλ)
// console.log( 3 % 5 ); // λλ¨Έμ§ μ°μ°μ 3
// console.log( 3 % 0 ); // NaN ( == Not a Number )
*/
</script>
μλ£ν( data type)
[ typeof μ°μ°μ ]
1. number
2. string
3. boolean
4. function
5. object - js array, js object
js νμ (μ ν)μ λμ μ νμ΄λ€.
var x; // typeof undefined
x = 5; // " number
x = "admin"; // " string
x = [] ; // object
x = function (){} // function
[μ§μ νκΈ°λ²]
let y = 123e5; //12300000;
y = 123e-5; //0.00123
[js μ μ,μ€μ = number]
let z = 100;
z = 3.14;
[typeof μ°μ°μ]
console.log( typeof 100 );
console.log( typeof (100) );
var xx; // undefined
var xx = ""; // string
// js λ°°μ΄ - λκ΄νΈ []
// let m = [ 1,2,3 ];
/*
let m = [];
m[0]=1;
m[1]=2;
m[2]=3;
*/
// js κ°μ²΄ - μ€κ΄νΈλ₯Ό μ¬μ©νλ€. {}
let p1 = {
name:"admin"
, age:20
};
console.log( p1.name );
console.log( p1["age"]);
[ ν€λ³΄λ κ΄λ ¨ μ΄λ²€νΈ ]
1. keydown μ΄λ²€νΈ : ν€λ³΄λλ₯Ό λλ₯Ό λ Ctrl, F1 μΈμ O, λμλ¬Έμ κ΅¬λΆ X, ν€λ³΄λ μ·¨μ κ°λ₯
2. keypress μ΄λ²€νΈ : ν€λ³΄λλ₯Ό λλ₯Ό λ Ctrl, F1 μΈμ X, λμλ¬Έμ κ΅¬λΆ O, ν€λ³΄λ μ·¨μ κ°λ₯
3. keyup μ΄λ²€νΈ : ν€λ³΄λ λμ λ
<style>
*{
box-sizing: border-box;
margin: 0;
padding: 0;
}
/* css μ νμ */
input[type=text]{
border: 1px solid grey;
padding: 5px;
font-size: 20px;
margin:10px;
width:90%;
}
/* νΉμ μν , :μμ¬ν΄λμ€ */
input[type=text]:focus{
background-color: yellow;
}
</style>
<!-- autofocus="autofocus" -->
<input type="text" id="txt01" tabindex="1" onkeyup="txt01_keyup();">
<br>
<input type="text" id="txt02" tabindex="2">
<script>
// global scope
var txtbox01 ;
let txtbox02;
window.onload = function (){
// μ΄κΈ°ν
// function scope
// var txtbox01 = document.getElementById("txt01");
txtbox01 = document.getElementById("txt01");
txtbox02 = document.getElementById("txt02");
txtbox01.focus();
// focus : μ΄μ μ λ§μΆ λ μΌμ΄λλ μ΄λ²€νΈ
// blur : μ΄μ μ μμ λ "
}
function txt01_keyup(){
/*
// μ΄λ²€νΈ μ 보 : event κ°μ²΄
// event.srcElement μ΄λ²€νΈκ° λ°μν κ°μ²΄
// console.log( event.type ); // "keyup"
let uniCode = event.keyCode ;
txtbox02.value = uniCode + "(" + String.fromCharCode(uniCode) +")";
*/
// μ
λ ₯μ λλΈ ν μν°ν€λ₯Ό λλ₯Ό λ....
if ( event.keyCode == 13 ) {
txtbox02.value = txtbox01.value;
} // if
} // txt01_keyup
</script>
νμ κ°―μ μ ν
<select id="count">
<!-- option[value=$]*15>{$} -->
</select>
γ΄ λμ μΆκ°
-js-
// selectIndex μμ±
// options 컬λ μ
// options[selctedIndex].value
// options[selctedIndex].text
// new μ°μ°μ
// Option κ°μ²΄ ( text, value)
var count = document.getElementById("count");
count.options[0] = new Option("μ ννμΈμ.", 0);
for (let i = 1; i <=15; i++) {
count.options[i] = new Option(i, i);
}
count.options[10].selected = true; // setAttribute("selected", "selected")
-jquery-
$("#count").append( $("<option value='0'>μ ννμΈμ.</option>") );
for (let i = 1; i <=15; i++) {
$("#count").append( $("<option value='"+i+"'>"+i+"</option>") );
} // for
$("#count option").eq(15).prop("selected", true);
νμ μ΄λ¦ μ λ ₯
<input type="text" id="name" name="name" autofocus="autofocus" >
<!-- onkeydown="name_keydown();" -->
<br>
<div id="demo">
</div>
γ΄ λμ μΆκ°
-js-
<script>
function name_keydown(){
var demo = document.getElementById("demo");
if( event.keyCode == 13 ){ // μν° μΉ λ
var name = document.getElementById("name");
// 1.
// demo.innerHTML += "<input type='checkbox' value='"+ name.value+"'>" + name.value +"<br>";
// 2. DOM μμ μμ± μΆκ°
var ckb = document.createElement("input"); // <input>
ckb.setAttribute("type", "checkbox"); // <input type='checkbox'>
ckb.setAttribute("value", name.value); // <input type='checkbox' value='μνν'>
demo.appendChild(ckb);
var textnode = document.createTextNode( name.value );
demo.appendChild(textnode);
var br = document.createElement("br");
demo.appendChild( br );
name.value = "";
name.focus();
} // if
} // function
</script>
-jquery-
<script>
$("#name").keydown(function() {
if( event.keyCode == 13 ){
var value = $(this).val();
//var content = "<input type='checkbox' value='"+ value +"'>" + value +"<br>";
//$("#demo").append($(content));
var $ckb = $("<input>").attr({
type:"checkbox",
value:value
})
var $text = $("<lable>"+ value +"</label>");
/*
$("#demo")
.append($ckb)
.append($text)
.append($("<br>"));
*/
$("#demo").append($ckb, $text, "<br>");
$("#name")
//.val("")
.select()
.focus();
} // if
});
</script>