[JavaScript] ๐ŸŽaddEventListenerํ•œ ์š”์†Œ์— ์—ฌ๋Ÿฌ๊ฐœ ์ด๋ฒคํŠธ ๋„ฃ๋Š” ๋ฒ•

์ „์ฃผ์€ยท2022๋…„ 12์›” 30์ผ
0
post-thumbnail

โœ” ํ˜‘์—…์„ ์œ„ํ•œ ๊ฐœ๋ฐœ๊ฐ€์ด๋“œ๋กœ ํŒ€์›์„ ์œ„ํ•ด ์ž‘์„ฑํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค.

โ› ๋ฐฉ๋ฒ•1: addEventListener

๐Ÿ‘€ ์›๋ฆฌํŒŒ์•…

ํ•œ์ค„์š”์•ฝ: ์ธ๋ผ์ธ๋ฐฉ์‹๊ณผ ํ”„๋กœํผํ‹ฐ ๋ฐฉ์‹์€ ์š”์†Œ ํ•˜๋‚˜๋‹น ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ ํƒ€์ž… ๋ฆฌ์Šค๋„ˆ๋งŒ ๋“ฑ๋ก ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— addEventListener ๋ฐฉ์‹์„ ์“ฐ์ž!

(์ฐธ๊ณ ) ์ด๋ฒคํŠธ๋ฅผ ์ฃผ๋Š” ๋ฐฉ๋ฒ•

<๋ฐฉ๋ฒ•1> ์ธ๋ผ์ธ ๋ฐฉ์‹: ์ด๋ฒคํŠธ๋ฅผ ์ด๋ฒคํŠธ ๋Œ€์ƒ์˜ HTMLํƒœ๊ทธ์— ์ง์ ‘ ์†์„ฑ์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

<input type="checkbox" onclick="getCheck()" value="button" />
<script>
  getCheck(){
  	...
  }
</script>

<๋ฐฉ๋ฒ•2> ํ”„๋กœํผํ‹ฐ ๋ฆฌ์Šค๋„ˆ ๋ฐฉ์‹: ์ด๋ฒคํŠธ ๋Œ€์ƒ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ์ฒด์— '.'์„ ์ฐ์–ด ํ”„๋กœํผํ‹ฐ๋กœ ์ด๋ฒคํŠธ๋ฅผ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ์‹

<input type="checkbox" value="button" class="check"/>
<script>
    document.querySelector(.check).onclick = function(){ 
  	...
  }
</script>

<๋ฐฉ๋ฒ•3> ์ด๋ฒคํŠธ ๊ฐ์ฒด ๋ฐฉ์‹: ie8 ์ดํ•˜ ๋ฒ„์ „์—์„œ๋Š” ์ด๋ฒคํŠธ ๊ฐ์ฒด๋ฅผ ํ•ธ๋“ค๋Ÿฌ์˜ ์ธ์ž๊ฐ€ ์•„๋‹ˆ๋ผ ์ „์—ญ๊ฐ์ฒด์˜ event ํ”„๋กœํผํ‹ฐ๋กœ ์ œ๊ณตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉฐ, target ํ”„๋กœํผํ‹ฐ๋„ ์ง€์›ํ•˜์ง€ ์•Š์•„ ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š” ๋ฐฉ์‹

<input type="checkbox" value="button" class="check"/>
<script>
    document.querySelector(.check).onclick = function(event){ 
  	...ie8์ดํ›„ ๋ฒ„์ „์—์„œ๋Š” event.target.value ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  }
</script>

โœจ<๋ฐฉ๋ฒ•4> addEventListener ๋ฐฉ์‹โœจ
(ํ•˜๋‚˜์˜ ์ด๋ฒคํŠธ ๋Œ€์ƒ์— ๋ณต์ˆ˜์˜ ๋™์ผ ์ด๋ฒคํŠธ ํƒ€์ž… ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋“ฑ๋ก๊ฐ€๋Šฅ)

<input type="checkbox" value="button" class="check"/>
<script>
const check = document.querySelector(.check)
check.addEventListener(click, getCheck(event)){
	...
});
check.addEventListener(click, getCheck(event)){
	...
});
</script>

๐ŸŽƒ ํ•„์š”ํ•œ ๊ฐœ๋…

  1. [javascript]์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ - addEventListener
    ์ฐธ๊ณ 1: https://nevertrustbrutus.tistory.com/310
    ์ฐธ๊ณ 2: MDN

  2. [css] ์„ ํƒ์ž
    ์ฐธ๊ณ : https://webty.tistory.com/60

  3. [javascript] document ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ
    ์ฐธ๊ณ : http://www.tcpschool.com/javascript/js_dom_document

    //์—ฌ๋Ÿฌ๊ฐœ์—๊ฒŒ ์ด๋ฒคํŠธ ์ค„ ๋•Œ: 
    document.querySelectorAll(์„ ํƒ์ž)
    //ํ•˜๋‚˜์—๊ฒŒ๋งŒ ์ด๋ฒคํŠธ ์ค„ ๋•Œ:
    document.querySelector(์„ ํƒ์ž)
  1. [jquery ๋ฌธ๋ฒ•]
    ์ฐธ๊ณ : https://soft91.tistory.com/9

  2. ์ด๋ฒคํŠธ ์ข…๋ฅ˜(Type) - click, scroll...
    ์ฐธ๊ณ 1: http://milooy.github.io/TIL/JavaScript/event.html
    ์ฐธ๊ณ 2: MDM

์˜ˆ์ œ (buy.html buy.css buy.js)

โ—jquery ์„ž์—ฌ ์žˆ์Œ ์ฃผ์˜! jquery ์“ธ ๋• head์— cdn์ถ”๊ฐ€ํ•˜๊ธฐ

<script src="https://code.jquery.com/jquery-3.6.3.min.js"
    integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" 
	crossorigin="anonymous"></script>

์•„๋ž˜ ์ฝ”๋“œ๋Š” ํฌ๊ฒŒ๋ณด๋ฉด input์— ๋Œ€ํ•ด ์ด๋ฒคํŠธ๊ฐ€ ๋‘๊ฐœ์žˆ๋‹ค!
document.querySelector('#bid_input').addEventListener('input', e=>{...})
document.querySelector('#bid_input').addEventListener('blur', e=>{...})

<input type="number" maxlength="10" placeholder="ํฌ๋ง๊ฐ€ ์ž…๋ ฅ" 
       autocomplete="off" id="bid_input" style="display:none">
<script>
//input: input/textarea์š”์†Œ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ
document.querySelector('#bid_input').addEventListener('input', e=>{
    let str_price=e.target.value;
    // ์ˆซ์ž ์™ธ ์ž…๋ ฅ ๋ถˆ๊ฐ€ (์ˆซ์ž๋„ 10๊ธ€์ž๊นŒ์ง€)
    if (str_price.length > e.target.maxLength){
        e.target.value = str_price.slice(0, e.target.maxLength);
    }
    // .price_now์— has_danger has_warning์ถ”๊ฐ€
    if(str_price < 30000 ){
        pricebox.classList.add('has_warning')
        pricebox.classList.add('has_danger')
        errormsg.style.display="block"
    } else {
        pricebox.classList.remove('has_warning')
        pricebox.classList.remove('has_danger')
        errormsg.style.display="none"
    }
});
//blur: ์š”์†Œ์— ํฌ์ปค์Šค๊ฐ€ ๋ฒ—์–ด๋‚ฌ์„ ๋•Œ ์ด๋ฒคํŠธ ๋ฐœ์ƒ
document.querySelector('#bid_input').addEventListener('blur', e=>{
    let str_price=e.target.value;
    // 30000์› ๋ฏธ๋งŒ ์ž…๋ ฅ์‹œ ๋‚ด์šฉ์ด ์ง€์›Œ์ง„๋‹ค.
    if(str_price < 30000 ){
        e.target.value='';
    }
    // 1000์› ๋‹จ์œ„๊ฐ€ ์•„๋‹ ์‹œ ๋‹ค๋ฅธ ๊ณณ ํด๋ฆญํ• ๋•Œ 1000์› ๋ฏธ๋งŒ๋‹จ์œ„ ๋ฒ„๋ฆผ
    if(str_price%1000 !=0){
        e.target.value=Math.floor(str_price/1000)*1000;
    }
    // ์ฆ‰์‹œ ๊ตฌ๋งค๊ฐ’๋ณด๋‹ค ๋น„์‹ธ๊ฒŒ ๋ถ€๋ฅด๋ฉด ์ฆ‰์‹œ๊ตฌ๋งค๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
    if(price_now < str_price){
        $(".header_main .title_txt").html("์ฆ‰์‹œ ๊ตฌ๋งคํ•˜๊ธฐ");
        $("#bid").removeClass("on");
        $("#now").addClass("on");
        $(".price_now").removeClass("active_input");
        $("#bid_input").hide();
        $("#now_price").show();
        $(".price_now_title").html("์ฆ‰์‹œ ๊ตฌ๋งค๊ฐ€");
        $(".deadline_info_area").hide();
        $(".step-1 .btn_confirm a").html("์ฆ‰์‹œ ๊ตฌ๋งค ๊ณ„์†");
        $(".step-1 .btn_confirm a").removeClass("disabled")
    }
    //1000๋‹จ์œ„ ๋ฒ„๋ฆผ์€ ๊ฐ™์ด ์ผ์–ด๋‚˜๊ธฐ ๋•Œ๋ฌธ์— ๋บ€๋‹ค 
  	//=> ๊ทธ๋ž˜์•ผ ํ•œ๋ฒˆ focusout ๋˜์—ˆ์„๋•Œ ํ•œ๋ฒˆ์— ์ฒ˜๋ฆฌ๋จ
      if((str_price >= 30000) &&(price_now > str_price)){
          $(".step-1 .btn_confirm a").removeClass("disabled")
      }
})
</script>     

0๊ฐœ์˜ ๋Œ“๊ธ€