[Day3] JS 3 [11/22] 

 

--๋ณต์Šต--

1. test01.html ํŽ˜์ด์ง€์—์„œ select ํƒœ๊ทธ์˜ option์„ ์„ ํƒํ•˜๋ฉด test01_ok.jsp ํŽ˜์ด์ง€๋กœ submit ๋˜์–ด์„œ  radio ๋ฒ„ํŠผ์ด ์ฒดํฌ๋˜๋„๋ก  js๋กœ ์ฝ”๋”ฉํ•˜์„ธ์š”~

 

[sample01.html]

-html-

<form action="sample01_ok.jsp" method="get">
	<select id="selsubject"  name="selsubject">
	   <option>์„ ํƒํ•˜์„ธ์š”.</option>    
	   <option value="kor">๊ตญ์–ด</option> 
	   <option value="eng">์˜์–ด</option> 
	   <option value="mat">์ˆ˜ํ•™</option>
	   <option value="pe">์ฒด์œก</option> 
	</select>
</form>

-(form ํƒœ๊ทธ ์žˆ์–ด์•ผ ์ž…๋ ฅํ•ด์„œ get, post ๊ฐ€๋Šฅ!)

 

-js-

    // 1. selsubject ์˜  option์„ ์„ ํƒํ–ˆ์„ ๋•Œ ์„œ๋ธŒ๋ฐ‹(submit)
    // 2. ๋ชจ๋“  ์ž…๋ ฅํƒœ๊ทธ๋“ค์€ form ํƒœ๊ทธ ์•ˆ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. 
    // 3. ?ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋ฆ„=๊ฐ’&ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋ฆ„=๊ฐ’ X
     document.getElementById("selsubject").onchange = function (){    	
    	var svalue = this.options[this.selectedIndex].value ;    	
    	if(svalue == "์„ ํƒํ•˜์„ธ์š”."){
    		alert("๊ณผ๋ชฉ๋งŒ ์„ ํƒํ•˜์„ธ์š”.");
    		return;
    	}
    	
    	// js ์„œ๋ธŒ๋ฐ‹~
    	// js ํ•จ์ˆ˜ : submit()
    	// http://localhost/webPro/javascript/days04
    	// /sample01_ok.jsp?ํŒŒ๋ผ๋ฏธํ„ฐ X
    	// ?selsubject=eng
    	// document.querySelector("form").submit();
    	// document.forms ์ปฌ๋ ‰์…˜ ,  options ์ปฌ๋ ‰์…˜
    	document.forms[0].submit();
    }

 

[sample01_ok.jsp]

-html-

<%
         // ?selsubject=eng
         String subject =  request.getParameter("selsubject");

          // [ jsp ์Šคํฌ๋ฆฝํŠธ 3๊ฐ€์ง€ ์ข…๋ฅ˜  ]
          // 1) ์Šคํฌ๋ฆฝํŠธ๋ฆฟ
%>

 <!-- 2) ํ‘œํ˜„์‹ ( ์ถœ๋ ฅ )-->
  subject = <%= subject %><br>

 [ ์„ ํƒ๋œ ๊ณผ๋ชฉ ] <br>
 <input type="radio" name="subject"   value="kor"><label>๊ตญ์–ด</label><br>
 <input type="radio"  name="subject"  value="eng"><label>์˜์–ด</label><br>
 <input type="radio"  name="subject"  value="mat"><label>์ˆ˜ํ•™</label><br>
 <input type="radio"  name="subject"  value="pe"><label>์ฒด์œก</label><br>

-js-

	var svalue =  "<%= subject %>" ;   // js ๋Š” ๋ฌธ์ž์—ด์€   ์Œ ํ™‘ ๋”ฐ์˜ดํ‘œ
	var radios = document.getElementsByName("subject"); 
	for (var i = 0; i < radios.length; i++) {
		if( radios[i].value ==  svalue ){
			radios[i].checked = true;
			break;   // return;
		} // if
	} // for

-jquery-

$(":radio[value='<%= subject %>']").prop("checked", true);

 * ์ฃผ์˜ : <%--  JSP ์ฃผ์„์ฒ˜๋ฆฌ  ***

 

2. โ–ถ, โ–ถโ–ถ ๋ฒ„ํŠผ์„ ๊ตฌํ˜„ํ•˜์„ธ์š” 

<style>
   select{
      height: 200px;
      width: 100px;
   }
   button{
      width:40px;
   }   
</style>
<table>
	<tr>
		<td>
				<select multiple="multiple" id="sleft">
				  <option>์ด์ฐฝ์ต</option>
				  <option>๊น€๊ฐ€์œจ</option>
				  <option>์ž„์ข…์œค</option>
				  <option>๋ฌธํ˜œ๋นˆ</option>
				  <option>์—„ํƒœํ˜</option>
				</select>
		</td>
		<td>
		   <button>โ–ถ</button><br>
		   <button>โ–ถโ–ถ</button><br>
		   <button>โ—€</button><br>
		   <button>โ—€โ—€</button><br>
		</td>
		<td>
		<select multiple="multiple" id="sright">
		</select>
		</td>
	</tr>
</table>

-js-

<script>
    // <button>โ–ถ ํด๋ฆญ
    document.querySelector("button:first-of-type").onclick = function (){
    	// 1. sleft ์—์„œ ์„ ํƒ๋œ(selected) option์˜ text, value ์–ป์–ด์™€์•ผ...
        var sleftOptions = document.querySelectorAll("#sleft option");
    	var sright = document.querySelector("#sright");
    	
    	// 2. sright ๋ชจ๋“  option ์ œ๊ฑฐ
    	// sright.options.length=0;
    	sright.innerHTML = "";
    	
        for (var i = 0, j=0; i < sleftOptions.length; i++) {
    		if( sleftOptions[i].selected ){
    			// console.log( sleftOptions[i].value +" / " + sleftOptions[i].text );
    			// sright  + option ์ถ”๊ฐ€
    			// sright.options[j++] = new Option(sleftOptions[i].value,sleftOptions[i].value); 
    			
    			// js   ๋ณต์ œ :  cloneNode() 
    			sright.appendChild( sleftOptions[i].cloneNode(true)  );
    		}
    	} // for
    } // 

    // select , option ์š”์†Œ ์ƒ์„ฑ, ์ œ๊ฑฐ
    //              createElement() / ์ œ๊ฑฐ ๋ฉ”์„œ๋“œ
    // 1)  sleft.remove(index);          ๋ฐ˜๋“œ์‹œ ๋’ค์— ์ธ๋ฑ์Šค ->
    
    // <button>โ–ถโ–ถ ํด๋ฆญ
    document.querySelector("button:nth-of-type(2)").onclick = function (){
    	// 1. sleft ์—์„œ ์„ ํƒ๋œ(selected) option์˜ text, value ์–ป์–ด์™€์•ผ...
        var sleftOptions = document.querySelectorAll("#sleft option");
    	var sright = document.querySelector("#sright");
    	
    	//sright.innerHTML = ""; 
    	// sright.options.length=0;
    	
        for (var i = 0, j=0; i < sleftOptions.length; i++) {
    		if( sleftOptions[i].selected ){
    			// console.log( sleftOptions[i].value +" / " + sleftOptions[i].text );
    			// sright  + option ์ถ”๊ฐ€
    			// sright.options[j++] = new Option(sleftOptions[i].value,sleftOptions[i].value);
    			
    			// jquery method : append()/ appendTo()
    			// js         method : appendChild()
    			sright.appendChild( sleftOptions[i] ); 
    		}
    	} // for
    } // 
</script>

-jquery-

<script>
// <button>โ–ถ ํด๋ฆญ
   $("button").first().click( function (){
	   // alert("a")
	   $("#sleft option:selected").each(function(i, element) {
		   // jquery ๋ณต์ œ ๋ฉ”์„œ๋“œ  : clone()
	   	   $("#sright").append(  $(element).clone() );
	   });
   });
   
   // <button>โ–ถโ–ถ ํด๋ฆญ
   $("button").eq(1).click( function (){
	   // alert("b")
	   $("#sleft option:selected").each(function(i, element) {
	   	   $("#sright").append(element);
	   });
   });
</script>

 

6. ์ฒซ ๋ฒˆ์งธ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ํ›„ ์—”ํ„ฐ ์น˜๋ฉด ๋‘ ๋ฒˆ์งธ ํ…์ŠคํŠธ ๋ฐ•์Šค๋กœ ํฌ์ปค์Šค ๋„˜๊ธฐ๊ณ 

   ๋‘ ๋ฒˆ์งธ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์ˆซ์ž๋ฅผ ์ž…๋ ฅ ํ›„ ์—”ํ„ฐ ์น˜๋ฉด ๋ง์…ˆ ๊ฒฐ๊ณผ๋ฅผ ์„ธ ๋ฒˆ์งธ ํ…์ŠคํŠธ ๋ฐ•์Šค์— ์ถœ๋ ฅํ•˜๋Š”  js ์ฝ”๋”ฉ์„ ํ•˜์„ธ์š”.  

 

<input type="text"  autofocus="autofocus" >
<!-- onkeydown="txt01_keydown();" -->
 + 
<input type="number" >
 = 
 <!-- 12e+5 -->
 <input type="text"  disabled="disabled">

-js-

   function txt01_keydown(){
	   // console.log( event.keyCode );  48(0) ~ 57(9)
	   if(  !(  event.keyCode >= 48 && event.keyCode <= 57  || event.keyCode == 8  || event.keyCode == 13 || event.keyCode == 229 )    ){
		   alert("์ž…๋ ฅ ์ž˜๋ชป!!!");
		   event.returnValue = false ;   // ์ด๋ฒคํŠธ ์ทจ์†Œ + ์ถœ๋ ฅ O
	   }
	   if( event.keyCode == 13 ){
		   document.querySelector("input[type=number]").focus();
	   }
   }
   
   document.querySelector("input[type=number]").onkeyup = function (){	
	   // alert("xxxx");
	   if( event.keyCode == 13 ){	   
		    var n1 = document.querySelector("input[type=text]:first-of-type").value;		    
		    // alert( typeof n1);  // js :  number   [string]  function  boolean  object		    
		    var n2 = this.value;
		    
		    // "12"  -> 12 ํ˜•๋ณ€ํ™˜
		    // java  :   Integer.parseInt( "12" )
		    // js      :   Number(),  parseFloat(),   parseInt(), eval()
		    var sum = Number(n1) + parseInt(n2);  // "12"+"23" = "1223"
		    
		    document.querySelector("input[type=text]:last-of-type").value = sum;
	   }
   }

-jquery-

   // $("input[type=text]")
   $(":text").first().keydown(function() {
   	  if ( event.keyCode == 13 ) {
		 $(this).next().focus();
	  } // 
   })
   
   $("input[type=number]").keydown(function() {
   	    if ( event.keyCode == 13 ) {
		    var n1 =  eval( $(":text").first().val() );
		    var n2 = parseInt( $(this).val() );
		    var sum = n1 + n2;
		    // $(":text").last()
		    $(this).next().val( sum );
	  } // 
   })

<style>
  table, th, td{
    border: 1px solid gray;
  }
  
  table{
     width: 50%;
     margin: 0 auto;
  }
</style>

 

<table>
  <thead>
	<tr>
		<td><input type="checkbox" class="all">๋ชจ๋‘ ์„ ํƒ</td>
		<td>Name</td>
		<td>Loc</td>
	</tr>
  </thead>	
  <tbody>
	<tr>
		<td><input type="checkbox" id="ck1"></td>
		<td>Animi.</td>
		<td>Hic.</td>
	</tr>
	<tr>
		<td><input type="checkbox" id="ck2"></td>
		<td>Esse.</td>
		<td>Fuga.</td>
	</tr>
	<tr>
		<td><input type="checkbox" id="ck3"></td>
		<td>Fuga.</td>
		<td>Ducimus?</td>
	</tr>
	<tr>
		<td><input type="checkbox" id="ck4"></td>
		<td>Beatae.</td>
		<td>Optio.</td>
	</tr>
  </tbody>
  <tfoot>
     <tr>
        <td colspan="3" align="center">
           <button>์„ ํƒ ํ™•์ธ</button>
        </td>
     </tr>
  </tfoot>	
</table>

<p id="demo">
   "์„ ํƒ ํ™•์ธ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•  ๋•Œ ์ฒดํฌ๋ฐ•์Šค์˜ id ์†์„ฑ๊ฐ’ +  [Name ์ปฌ๋Ÿผ]์˜ ๊ฐ’๋„ ๋ชจ๋‘ ์ถœ๋ ฅ 
   ck1, ch3, ch4
</p>

-js-

<script>
   document.querySelector("tfoot  button").onclick = function (){
	   // alert("1");
	   // document.querySelectorAll("input[type=checkbox].not(.all)")
	   var ckbs = document.querySelectorAll("tbody input[type=checkbox]");
	   var pdemo = document.querySelector("#demo");
	   var content = "";
	   for (var i = 0; i < ckbs.length; i++) {
		     if( ckbs[i].checked ){
		    	
		    	 // DOM ์š”์†Œ(๋…ธ๋“œ) ํƒ์ƒ‰
		    	 // console.log(  ckbs[i].parentElement.parentElement.childNodes ); // ๋…ธ๋“œ ์ปฌ๋ ‰์…˜
		    	 var name = ckbs[i].parentElement.parentElement.children[1].innerText ;  // ์š”์†Œ ์ปฌ๋ ‰์…˜
		    	 // ckbs[i].parentNode
		    	 
		    	 content += ckbs[i].id + " / " + name + "<br>";
		     }
	    }  // for
	    pdemo.innerHTML = content;
   }
</script>

-jquery-

<script>
   $("tfoot button").click( function (){
	   // jquery selector   :checkbox  == input[type=checkbox]
       var content = "";
	   $("tbody :checkbox:not(.all):checked").each(function(i, element) {
	   	   var id =  element.id ;
	   	   var name = element.parentElement.nextElementSibling.innerText;
	   	    content += "<li>"+  id + " / " + name + "</li>";
	   	    
	   	   /*
	   	   $("#demo").html( function ( index, oldhtml){
	   		   // return oldhtml +"<li>"+ id +" / " + name + "</li>";
	   		   return oldhtml + $("<li></li>").text(  id +" / "+ name );
	   	   });
	   	   */
	   });
	   
	   $("#demo").html(  content  ) ;
   } );
</script>

 

-๋ชจ๋“ ๋ฐ•์Šค ์ฒดํฌ๋ˆ„๋ฅด๋ฉด ๋‹ค ์ฒดํฌ & ์ฒดํฌ ํ•˜๋‚˜ ํ’€๋ฉด ๋ชจ๋‘์ฒดํฌ ํ’€๊ธฐ-

<script>
   $(":checkbox.all").click( function (){
	   $(":checkbox:not(.all)").prop("checked",  $(this).prop("checked") );
   });
   
   $(":checkbox:not(.all)").click( function (){
	    //  ์ฒดํฌ๋œ ์ฒดํฌ๋ฐ•์Šค์˜ ๊ฐฏ์ˆ˜
	    var totalCount = $(":checkbox:not(.all)").length;
	    var checkedCount = $(":checkbox:not(.all):checked").length;
	    
	    $(":checkbox.all").prop("checked", checkedCount == totalCount ? true: false);
	    /*
	    if(   checkedCount == totalCount  ){
	    	$(":checkbox.all").prop("checked", true);
	    }else{
	    	$(":checkbox.all").prop("checked", false);
	    }
	    */
   });
</script>

js ํ•จ์ˆ˜

  1. js ํ•จ์ˆ˜ ๊ฐœ๋… == ์ž๋ฐ” ํ•จ์ˆ˜ ๊ฐœ๋…

     - ์„ (line) ๊ธ‹๊ธฐ : 50๋ฒˆ  X  50์ค„์ฝ”๋”ฉ         drawLine() ์„ ์–ธ -> ํ˜ธ์ถœ,  ์˜ค๋ฒ„๋กœ๋”ฉ(์ค‘๋ณตํ•จ์ˆ˜)

  2. js ํ•จ์ˆ˜ ์„ ์–ธ ํ˜•์‹

function ํ•จ์ˆ˜๋ช…([๋งค๊ฐœ๋ณ€์ˆ˜...]){
  [return ๋ฆฌํ„ด๊ฐ’;]
}

   3. js ํ•จ์ˆ˜ ํ˜ธ์ถœ 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•..

         1) on์ด๋ฒคํŠธ๋ช… ์†์„ฑ = "jsํ•จ์ˆ˜();"

         2) js ์ฝ”๋“œ์—์„œ  jsํ•จ์ˆ˜ ํ˜ธ์ถœ

         3) ์ž๋™( ์ž์ฒด ํ˜ธ์ถœ)  X -> "js ํด๋กœ์ €"

 

<button onclick="btn1_click();">๋ฒ„ํŠผ 1</button><br>
<button onclick="btn2_click();">๋ฒ„ํŠผ 2</button><br>
<script>
   // ๋‘ ์ •์ˆ˜์˜ ํ•ฉ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” sum ํ•จ์ˆ˜ ์„ ์–ธ
   /*
   public static int sum(int a, int b){
	   return a+b;
   }
   */
   function sum(a,b){
	   // ํ•จ์ˆ˜ ์•ˆ์— ์„ ์–ธ๋œ ๋ณ€์ˆ˜ : ์ง€์—ญ๋ณ€์ˆ˜
	   
	   return a+b;
   }

   function btn1_click(){
	   var result =  sum(10,20);
	   alert(   result  );
   }
   
   function btn2_click(){
	   //
	   //
	   // btn1_click();
	   // js ์ž๋ฃŒํ˜• :  number, string, boolean, function, object
	   
	   //var fnhap = sum;
	   var fnhap = function (a, b){
		   return a+b;
	   }
	   
	   var result =  fnhap(1,2);
	   alert(   result  );
   }
   
   // btn1_click(); // ํ•จ์ˆ˜ ํ˜ธ์ถœ
   
   (function (){  // ์ต๋ช… ํ•จ์ˆ˜
	   //
	   //
   })();
</script>
   function btn1_click(){
	   // var name = "ํ™๊ธธ๋™";	    // ํ•จ์ˆ˜ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉ๋˜๋Š” ์ง€์—ญ๋ณ€์ˆ˜
	   name = "์˜ค์Šน์ฃผ";            // var, let, const, X  ์ „์—ญ๋ณ€์ˆ˜
	   alert( "1 : " +  name );
   }
   
   function btn2_click(){
	   alert( "2 : " +  name );
	   // alert( "2 : " +  window.name );
   }

undefined์™€ null ๋น„๊ต

    var msg;
    // if( msg == null ){}
    
    // alert(  typeof msg );   // undefined ํƒ€์ž…
    // alert( typeof null);      // object ํƒ€์ž…
    
    // alert(    undefined  ==  null  ); // true
    // alert(    undefined  ===  null  ); // ๊ฐ’ +  ํƒ€์ž…
  //var person ;           // undefined ํƒ€์ž…
  //var person = null;  // object ํƒ€์ž…
  
  var name = "";        // string ํƒ€์ž…
  var name ;               //  undefined ํƒ€์ž… == ์•„์ง ๊ฐ’์ด ํ• ๋‹น๋˜์ง€ ์•Š์•„ ํƒ€์ž…์ด ๊ฒฐ์ •๋˜์ง€ ์•Š์•˜๋‹ค.

 js ๊ฐ์ฒด(๊ฐœ์ฒด,object)

   // 1. js ๊ฐ์ฒด               {} ์ค‘๊ด„ํ˜ธ                           - ๋ฐฐ์—ด  []๋Œ€๊ด„ํ˜ธ
   //    js ๊ฐ์ฒด๋Š” ์†์„ฑ์ด๋ผ๊ณ ํ•˜๋Š” ๋ช…๋ช…๋œ ๊ฐ’๋“ค์˜ ์ง‘ํ•ฉ(์ปจํ…Œ์ด๋„ˆ)์ด๋‹ค. 
   
   const person = {
	   name:"admin" , 
	   age:20,
	   dispInfo:function (){
		   return this.name + ", " + this.age;  
		   // ์ด๋•Œ this๋Š” js ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. 
	   }
   };
   
   console.log(   person.name   );  // objectName.propertyName;
   console.log(   person["name"]   ); // objectName["propertyName"]
   console.log(   person.dispInfo()   );

์—„๊ฒฉ๋ชจ๋“œ ์„ ์–ธ (๋ณ€์ˆ˜๋Š” ๊ผญ ์„ ์–ธํ•˜๊ณ  ์‚ฌ์šฉํ•˜๊ฒ ๋‹ค)

"use strict";


this์˜ ์‚ฌ์šฉ์ฒ˜ ๋”ฐ๋ฅธ ์˜๋ฏธ

- js ์—์„œ๋Š” [ this ํ‚ค์›Œ๋“œ ]๊ฐ€ ์‚ฌ์šฉ๋˜๋Š” ๊ณณ(๋ฐฉ๋ฒ•)์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ( ๊ธฐ์–ต )

       //(1) [object Window]          BOM ์ตœ์ƒ์œ„ ๊ฐ์ฒด
       alert( this ); 
       
       // (2) ํ•จ์ˆ˜ ์•ˆ์—์„œ              this    [object Window] 
       //                   ์—„๊ฒฉ๋ชจ๋“œ       this    [undefined]
       function test(){
    	   alert( this );
       }
       test();
       
       // (3) ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ(์ˆ˜์‹ ๊ธฐ)์—์„œ this
       function h3_click(){
    	   alert( this )
       }
       
       // (4) person ๊ฐ์ฒด ๋‚ด์˜  this ๋Š” ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ (  object )
       var person = {
    		   name:"admin"
    		   , dispInfo: function (){
    			   //  return this.name ;
    			   alert( this );  // [object Object]
    		   }
       }
       
       person.dispInfo();  // [object Object]
<!-- ์ด๋•Œ this๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์€ ๊ฐ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.  [object HTMLHeadingElement] -->
<h3 onclick="alert( this );">ex05.html - ์—„๊ฒฉ๋ชจ๋“œ ์„ ์–ธ,  this์˜ ์‚ฌ์šฉ์ฒ˜ ๋”ฐ๋ฅธ ์˜๋ฏธ, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ</h3>

์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

1) on์ด๋ฒคํŠธ๋ช…=""

2) document.getElementById("demo").on์ด ๋ฒคํŠธ๋ช… = function (){}     

3) document.getElementById("demo").addEventListener("click", function(e) { } );


js ๋ฌธ์ž์—ด

    var msg = "AB\"CD\"EFGHIJKLMNOPQRSTUVWXYZ";
    
    // 1. ๋ฌธ์ž์—ด ๊ธธ์ด -  length ์†์„ฑ
    console.log( "1. msg ๋ฌธ์ž์—ด ๊ธธ์ด : " +   msg.length );  // 28
    
    // 2. ๋‘ ๋ฌธ์ž์—ด ๋น„๊ต  -    ==  !=    ===  !==  ์—ฐ์‚ฐ์ž. 
    var name1 = "ํ™๊ธธ๋™";                   // string
    // var name2 = "ํ™๊ธธ๋™";
    var name2 = new String("ํ™๊ธธ๋™");   // object        // String
    alert( typeof name1); // string
    alert( typeof name2); // object
    // java     equals()
    // js          ==
    console.log(  name1 == name2 );  // true                             true
    console.log(  name1 === name2 );  // true        ๊ฐ’ + ํƒ€์ž…      false       !==

๊ฐœํ–‰์ œ์–ด๋ฌธ์ž

    \n 
    \t
    \b
    \f
    \r

๋ฉ”์„œ๋“œ

   var msg = "     hello world     ";
   // console.log(  "["+ msg.trim() +"]" );
   
   var str1 = "hello",  str2 ="world";
   // ๋‘ ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ(๊ฒฐํ•ฉ) :  + ๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž. 
   // var str3 = str1 + " " + str2;
   // var str3 =  str1.concat("-").concat(str2);
   var str3 =  str1.concat("-", str2, "์—ฐ๊ฒฐ๋ฌธ์ž์—ด");
   // console.log( str3 );

๋Œ€๋ฌธ์ž๋ณ€ํ™˜

    msg = "Hello World!!!";
    // ๋Œ€๋ฌธ์ž ๋ณ€ํ™˜
    //console.log(   msg.toUpperCase()  );  // HELLO WORLD!!!
    //console.log(   msg.toLowerCase()  );  // hello world!!

ํŒ€์›์ด๋ฆ„  :
<input type="text" value="๊น€๊ฐ€์œจ(ํŒ€์žฅ), ์กฐ๋ฏผ๊ฒฝ(๊ธฐ์ˆ ๊ณ ๋ฌธ), ์ž„ํฌ์ง„, ์ด์žฌ๋ฏผ, ์—„ํƒœํ˜, ํ™ฉ์ˆ˜๋นˆ, ๋ฐ•ํ˜œ์€">

<div id="demo">
   <!-- <li>๊น€๊ฐ€์œจ</li>
   <li>์กฐ๋ฏผ๊ฒฝ</li> -->
</div>
                                          // :text   jquery selector
   document.querySelector("input[type=text]").onkeydown = function (){
	   if( event.keyCode == 13 ){
		   // 1. ํ…์ŠคํŠธ๋ฐ•์Šค์—์„œ ์ž…๋ ฅ๋ฐ›์€ ํŒ€์›๋ช…์„ ์–ป์–ด์™€์„œ.
		   var team = this.value;
		   // console.log( team );
		   // 2. ๊ตฌ๋ถ„์ž ์ฝค๋งˆ(,)๋กœ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ๋‚ด๊ธฐ.
		   // 3. js ๋ฐฐ์—ด ์ €์žฅ
		   var teamList = team.split(",");
		   // 4. js ๋ฐฐ์—ด์„ for/ ๋ฐ˜๋ณต๋ฌธ ์‚ฌ์šฉํ•ด์„œ <li>๊น€๊ฐ€์œจ</li>
		   // 5. <div id="demo"> ์ž์‹์œผ๋กœ ์ถ”๊ฐ€,  innerHTML ์„ค์ •.
		   var demo = document.querySelector("#demo");
		 
		   /*
		   // DOM 
		   for (var i = 0; i < teamList.length; i++) {
			  // console.log(  teamList[i].trim()  );
			  var li = document.createElement("li");
			  li.innerText = teamList[i].trim();			  
			  demo.appendChild( li );
		    } //for
		   */
		   
		   var content = "<ol>";
		   for (var i = 0; i < teamList.length; i++) {
			   content += "<li>";
			   content += teamList[i].trim();	
			   content += "</li>";
		   } // 
		   content += "</ol>";
		   // <ol><li>๊น€๊ฐ€์œจ(ํŒ€์žฅ)</li><li>์กฐ๋ฏผ๊ฒฝ(๊ธฐ์ˆ ๊ณ ๋ฌธ)</li><li>์ž„ํฌ์ง„</li><li>์ด์žฌ๋ฏผ</li><li>์—„ํƒœํ˜</li><li>ํ™ฉ์ˆ˜๋นˆ</li><li>๋ฐ•ํ˜œ์€</li></ol>
		   // alert( content );
		   
		   demo.innerHTML = content;

js  ์ •๊ทœํ‘œํ˜„์‹

   // 1. ์ƒ๋…„์›”์ผ
   // 2. ์„ฑ๋ณ„
   // 3.  ๋งŒ๋‚˜์ด
   // 4. ๊ฒ€์ฆ
   var rrn = "781222-170001";   
   // console.log( rrn.length ); // 14
   // ์ˆซ์ž6-์ˆซ์ž7                                   true/false   rrn.matches( ์ •๊ทœํ‘œํ˜„์‹)
   // [ js   ์ •๊ทœํ‘œํ˜„์‹ ] - ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ : search(), replace(),  test(), exec()
   //   ํ˜•์‹)                      /pattern/modifiers
   //                                 /[0-9]{6}-\d{7}/i              [i,g,m]
   // pattern = /์ •๊ทœํ‘œํ˜„์‹/๋ชจ๋“œ ;
   // boolean pattern.test( ๋ฌธ์ž์—ด)
   var pattern =    /^[0-9]{6}-\d{7}$/                ;  
   // console.log(  pattern.test(rrn) ); 
   //            /^[0-9]{6}-\d{7}$/.test( rrn )
   
   // ์ƒ์ผ ๋…„๋„
   // ์ƒ์ผ ์›”
   //console.log( rrn.substring(2, 4)  );  // to
   //console.log( rrn.substr(2, 2)  );  // length
   //console.log( rrn.slice(2, 4) );
   
   // ์ฃผ๋ฏผ๋“ฑ๋ก๋ฒˆํ˜ธ ๋’ท์ž๋ฆฌ 7 ์ถœ๋ ฅ.
   console.log(  rrn.substring( 7 ) );  // 170001
   console.log(  rrn.substring( 7, 14 ) );  // 170001
   console.log(  rrn.substring( 7, rrn.length ) );  // 170001
   
   console.log( rrn.substr(7)  );  // from   [, length]
   console.log( rrn.substr(7, 7)  );  // from , length
   
   console.log( rrn.slice(7)  );
   console.log( rrn.slice(7, 14)  );   // from , to
   // ์ฐจ์ด์ 
   console.log( rrn.slice(-6)  );  // ์Œ์ˆ˜:๋’ท ์ž๋ฆฌ์ˆ˜
   
   
   // ์ƒ์ผ ์ผ
   // ์„ฑ๋ณ„
   //rrn.substring(from, to);
   //rrn.substr(from, length);
   // rrn.slice(from, to);
<script>
   var input = "\nIs  Th \nis it?";
//   var pattern =   /^is/;
//   var pattern =   /^is/i;
   var pattern =   /^is/m;              /*  [m]ultiline */
   //console.log(  pattern.test( input ) );
</script>

<script>
   var input = "Visit Microsoft!";
   // var pattern = "Microsoft";  // string
   // var pattern = "microsoft";
   var pattern = /microsoft/i;     // regexp
   var index =  input.search(pattern);
   //console.log( index );  // 6  ,            -1   
   //console.log(  input.replace(pattern,  "XXX") );  // Visit XXX!
</script>

js ๋ฌธ์ž์—ด ๋ฉ”์„œ๋“œ

   var msg = "์•ˆ๋…•ํ•˜์„ธ์š”, ํ™๊ธธ๋™ ์ž…๋‹ˆ๋‹ค. -ํ™๊ธธ๋™ ์™ˆ - ";
   
   // var pattern = "ํ™๊ธธ๋™"; // string
   var pattern = /ํ™๊ธธ๋™/g ;             // regexp             i    m    g
   var replacement = "YYY";
   console.log(  msg.replace(pattern, replacement)  ); 
   
   // msg.replaceAll(target)
   
   // msg.indexOf("ํ™๊ธธ๋™", 0);
   console.log(   msg.indexOf("ํ™๊ธธ๋™")  ); // 7
   
   // msg.lastIndexOf(elt, from)

   //console.log(  msg.charAt(0) );
   // console.log(  msg.charAt(1) );
   
   for (var i = 0; i < msg.length; i++) {
	  //  console.log(  msg.charAt(i) );
  }
    // "7"  ->  7 ํ˜•๋ณ€ํ™˜ ?       Java :  Ineger.parseInt()
   //                                      js    : Number() , parseInt(), parseFloat(), eval()
   //  7   -> "7"  ํ˜•๋ณ€ํ™˜ ?     7 + ""          "7",            new String(7)
    /*
     let no = 7 + "";
     //    "0007"              String.format("%04d", 7)    "0007" StringFormat 
     no = no.padStart(4, "0");
     console.log( no );   //  "0007"
     */
     
     let no = "17";
     var len = no.length ;
     
     for (var i = 0; i < len ; i++) {
		no = "0" + no;
	}  
    //  console.log(  i + " : " + no );	
     
     // padStart() , padEnd()   ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฉ”์„œ๋“œ(2017)         -   ES5(2015)
   var msg = "    hello world    ";
   console.log( "["+ msg.trim() +"]" );           // [hello world]
   console.log( "["+ msg.trimStart() +"]" );  // [hello world    ]
   console.log( "["+ msg.trimEnd() +"]" );     // [    hello world]
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ์— ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ ํ•˜๊ธฐ