WEB/JQuery

[JQuery] on() , one(), off()

애플쩀 2022. 6. 14. 10:30
js JSON+js AJAX+ jquery AJAX   

$("button").이벤트명(함수)   
$("button").click(function(){         });   

on()   이벤트 등록 
one()  이벤트가 한번만 발생    
off()   이벤트 제거
 //버튼 누르면 t를 숨겼다가 나타났다가 

<script>
/* $("button").one("click",function(event){
alert("클릭됨");
});
 */
$("button").on("click",function(event){
    if($(this).text()=="hide"){
        $("p").hide();
        $(this).text("show");
    }else{
        $("p").show();
        $(this).text("hide");
    }  });
 

  $("button").on("click",function(event){
    var text= $(this).text;
    $("p").toggle("show",function(){
        $("button").html(text=="hide"?"show":"hide");
    });
});

</script>
2022.06.14 10:06:42

ex05.html

    js JSON+js AJAX+ jquery AJAX

    $("button").이벤트명(함수)
    $("button").click(function(){     
    });
    on()
    one() 이벤트가 한번만 발생 
    off()

 

 

2

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>off demo</title>
  <style>
  button {
    margin: 5px;
  }
  button#theone {
    color: red;
    background: yellow;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
</head>
<body>
 
<button id="theone">Does nothing...</button>
<button id="bind">Add Click</button>
<button id="unbind">Remove Click</button>
<div style="display:none;">Click!</div>
 
<script>
function flash() {
  $( "div" ).show().fadeOut( "slow" );
}

//두번째버튼클릭첫번째 버튼의 클릭이벤트 on() 메소드 등록 
$( "#bind" ).click(function() {
   // $( "#theone").on( "click", flash )
  $( "body" )
    .on( "click", "#theone", flash )
    .find( "#theone" )
      .text( "Can Click!" );
});

//세번째버튼 클릭: 첫번째 버튼의 클릭이벤트 off() 메소드 제거 
$( "#unbind" ).click(function() {
  $( "body" )
    .off( "click", "#theone", flash )
    .find( "#theone" )
      .text( "Does nothing..." );
});
</script>
 
</body>
</html>