'프로그램/jquery, javascript'에 해당되는 글 44건

  1. 2015.10.11 jquery 모달 팝업창 만들기 / javascript modal popup layer / 모달 레이어 구현 / div modal 팝업창 정 가운데 띄우기 / modal dialog window
  2. 2015.09.24 jquery 레이어 팝업창 만들기 / 레이어 가운데 정렬 / div 정중앙 위치 / layer popup center 띄우기
  3. 2015.09.23 jquery 불투명 배경 레이어 띄우기 / javascript layer popup / modal popup window / jquery 모달 팝업창 만들기 구현
  4. 2015.08.02 jquery json 문자열 post 전송하기 / jquery 배열 전송 / json post 전달 / jquery 배열 ajax post / jquery 키 값 배열 post 전송하기
  5. 2015.08.02 jquery json 객체 post 전송하기 / javascript json 객체 전송 / jquery 배열 전송 / json post 전달 / jquery 배열 ajax post / javascript 키 값 배열 post 전송하기 / javascript 임의 키 값 배열 전송
  6. 2015.08.02 jquery 배열 전송 / json post 전달 / ajax array 전송 / javascript 배열 post 전송하기
  7. 2015.06.26 jquery img error 처리 / $("img").error() 해결 / 이미지 엑박 / 이미지 없을 때 / img 에러 / 이미지 오류 / 이미지 엑박 / img 태그 엑박 방지 1
  8. 2015.03.17 jquery scroll 따라다니는 위로 버튼 / 모바일웹 스크롤 배너 / layer 이동
  9. 2015.03.16 jquery 스크롤(scroll) 따라다니는 배너 레이어 / 위로 버튼 / 화면 상단으로 이동 / scroll layer 이벤트 버튼
  10. 2015.03.16 jquery 동적 이벤트 추가 제어하기 / jquery ajax selector / dom 추가 생성 / table 태그 추가

jquery 를 이용해서 모달을 구현하고 브라우저 창 화면의 한 가운데에 띄우며

창 크기가 변경되어도 한 가운데에 계속 위치해 있다.

배경은 불투명으로 처리하며, 모달이나 불투명 배경을 클릭하면 닫는다.

 

 

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>okkks.tistory.com</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
.opacity_bg_layer {display:none;position:absolute; top:0; left:0; width:100%; height:100%; background:#000; opacity:.5; filter:alpha(opacity=50); z-index:10;}
.layer_pop_center {position:fixed;background:yellowgreen;padding: 10px;z-index:11;}
</style>
<script type="text/javascript">
/* 불투명 배경 레이어 뛰우기 */
function opacity_bg_layer() {
  if(!$('.opacity_bg_layer').length) {
    $('<div class="opacity_bg_layer"></div>').appendTo($('body'));
  }

  var oj = $(".opacity_bg_layer");

  // 화면의 가로, 세로 알아내기
  var w = $(document).width();
  var h = $(document).height();

  oj.css({'width':w,'height':h}); // 불투명 배경 레이어 크기 설정
  oj.fadeIn(500); // 불투명 배경 레이어 보이기 속도
}

 

/* 레이어 생성
cls : class
cont : 내용
기본 : 숨기기
*/
function layer_pop_crt(cls, cont) {
  if(!cls) return false;
  if(!$(cls).length) {
    $('<div class="' + cls + '">' + cont + '</div>').appendTo($('body'));
  }

  return true;
}

 

/* 레이어 띄우기
oj : 레이어 객체
*/
function layer_pop_center(oj) {
  if(!oj.length) return false;
  oj.layer_pop_center_set();
}

 

/* 레이어 팝업 위치 설정 */
$.fn.layer_pop_center_set = function () {
    //this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
    //this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");

    this.css("top", ($(window).height() - $(this).outerHeight())/2);
   this.css("left", ($(window).width() - $(this).outerWidth())/2);

    return this;
}

 

/* 레이어 닫기 후 삭제
oj : 레이어 객체
*/
function layer_pop_close(oj) {
  if(oj.length) {
    oj.fadeOut(500, function() {
      oj.remove();
    });
  }

  // 불투명 배경 레이어 삭제
  var oj = $('.opacity_bg_layer');
  if(oj.length) {
    oj.fadeOut(500, function() {
     oj.remove();
    });
  }
}

 

/* 화면을 불러온 후 처리 */
$(document).ready(function() {
  var ly = 'layer_pop_center';
  var _ly;
  var ly_bg = $('.opacity_bg_layer');

  $(document).on('click', '.layer_pop_center_show', function() { // 레이어 팝업 띄우기
    if(!ly_bg.length) opacity_bg_layer(); // 불투명 배경 레이어 띄우기
    var str_html = "okkks.tistory.com"; // 레이어 팝업 내용
    if(layer_pop_crt(ly, str_html)) {
      _ly = $('.' + ly); // 레이어 팝업 생성 후 재 선언
      layer_pop_center(_ly);
      _ly.fadeIn(500);
    } else {
      if(ly_bg.length) ly_bg.remove();
    }
  }).on('click', '.opacity_bg_layer', function() { // 불투명 배경 레이어를 클릭하면 닫기
    layer_pop_close(_ly);
  }).on('click', '.layer_pop_center', function(e) { // 레이어 팝업 닫기
    if(_ly.length) layer_pop_close(_ly);
  });
});

 

// 브라우저 창 크기 변경에 따른 처리
$(window).resize(function() {
  var oj = $('.layer_pop_center');
  if(oj.length) layer_pop_center(oj); // 레이어 팝업이 실행된 상태에서만 진행
  if($('.opacity_bg_layer').length) opacity_bg_layer(); // 불투명 배경 레이어가 실행된 상태에서만 진행
});
</script>
</head>
<body>
 <div><a href="http://okkks.tistory.com/1094" target="_blank">okkks.tistory.com</a></div>
 <span><a href="http://okkks.tistory.com/1095" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com/1094" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com/1095" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com/1094" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com/1095" target="_blank">okkks.tistory.com</a></span>
 <div><a href="http://okkks.tistory.com/1094" target="_blank">okkks.tistory.com</a></div>
 <div><a href="#" class="layer_pop_center_show">모달 팝업 창 띄우기</a></div>
 <div><a href="http://okkks.tistory.com/1095" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com/1094" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com/1095" target="_blank">okkks.tistory.com</a></div>
</body>
</html>



 

더보기>

- jquery 불투명 배경 레이어 띄우기 / javascript layer popup / modal popup window / jquery 모달 팝업창 만들기 구현

- jquery 레이어 팝업창 만들기 / 레이어 가운데 정렬 / div 정중앙 위치 / layer popup center 띄우기

Posted by 은둔고수

div 레이어를 화면의 한가운데에 팝업창처럼 띄우기로

브라우저 창 크기가 변경되어도 정중앙에 계속 위치해 있다.

 

 

 

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>okkks.tistory.com</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
.layer_pop_center {background:yellowgreen;padding: 10px;}
</style>
<script type="text/javascript">
/* 레이어 생성
cls : class
cont : 내용
기본 : 숨기기
*/
function layer_pop_crt(cls, cont) {
 if(!cls) return false;
 if(!$(cls).length) {
  $('<div class="' + cls + '">' + cont + '</div>').appendTo($('body'));
 }

 return true;
}

 

/* 레이어 팝업
oj : 레이어 객체
*/
function layer_pop_center(oj) {
 if(!oj || !$(oj).length) return false;
 oj.layer_pop_center_set();
}

 

/* 레이어 팝업 위치 설정 */
$.fn.layer_pop_center_set = function () {
 this.css("position", "fixed");
    this.css("top", Math.max(0, (($(window).height() - $(this).outerHeight()) / 2) + $(window).scrollTop()) + "px");
    this.css("left", Math.max(0, (($(window).width() - $(this).outerWidth()) / 2) + $(window).scrollLeft()) + "px");
    this.css("z-index", "11");
    return this;
}

 

/* 레이어 닫기 후 삭제
ly : 레이어 (예> '.layer_pop_center')
*/
function layer_pop_close(ly) {
 if($(ly).length) {
  $(ly).fadeOut(500, function() {
   $(ly).remove();
  });
 }
}

 

/* 화면을 불러온 후 처리 */
$(document).ready(function() {
 var ly = 'layer_pop_center';
 var ly_ = '.' + ly;

 $(document).on('click', '.layer_pop_center_show', function() {
  var str_html = "레이어 한가운데 띄우기"; // 레이어 팝업 내용
  if(layer_pop_crt(ly, str_html)) {
   layer_pop_center($(ly_));
   $(ly_).fadeIn(500);
  }
 }).keyup(function(e) { // 레이어 팝업 닫기 : esc 키
  if(e.keyCode == 27) layer_pop_close(ly_);
 }).on('click', '.layer_pop_center', function(e) { // 레이어 팝업 닫기
  if($(ly_).length) layer_pop_close(ly_);
 });
});

 

// 브라우저 창 크기 변경에 따른 처리
$(window).resize(function() {
 var ly = '.layer_pop_center';
 if($(ly).length) layer_pop_center($(ly)); // 레이어 팝업이 실행된 상태에서만 진행
});
</script>
</head>
<body>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="#" class="layer_pop_center_show">레이어 한가운데 띄우기</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
</body>
</html>



더보기>

- jquery 불투명 배경 레이어 띄우기 / javascript layer popup / modal popup window / jquery 모달 팝업창 만들기 구현

- jquery 모달 팝업창 만들기 / javascript modal popup layer / 모달 레이어 구현 / div modal 팝업창 정 가운데 띄우기 / modal dialog window

Posted by 은둔고수

jquery를 이용해서 화면 전체를 덮는 불투명한 레이어 팝업창 띄우기로

화면을 불러왔을 때와 띄우기 버튼 등을 클릭했을 때 실행된다.

 

 

                                                                     [화면을 불러오면 바로 실행]

 

 

 

 

[스크롤을 이동해도 가려진 부분도 포함]

 

 

 

[불투명 배경 레이어의 아무 곳이나 클릭하면 닫기(ESC 키 포함)]

 

 

 

["배경용 불투명 레이어 띄우기"를 클릭해서 띄우기]

 

 

 

["배경용 불투명 레이어 띄우기"를 클릭해서 띄우기]

 

 

 

<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8"/>
 <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>okkks.tistory.com</title>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<style>
.opacity_bg_layer {display:none;position:absolute; top:0; left:0; width:100%; height:100%; background:#000; opacity:.5; filter:alpha(opacity=50); z-index:10;}  // 불투명 레이어 CSS 설정
</style>
<script type="text/javascript">
/* 불투명 배경 레이어 뛰우기 */
function opacity_bg_layer() {
 if(!$('.opacity_bg_layer').length) { // 불투명 배경 레이어가 없으면 생성
  $('<div class="opacity_bg_layer"></div>').appendTo($('body'));
 }

 var oj = $(".opacity_bg_layer");

 // 화면의 가로, 세로 알아내기
 var w = $(document).width();
 var h = $(document).height();

 oj.css({'width':w,'height':h}); // 불투명 배경 레이어 크기 설정
 oj.fadeIn(500); // 불투명 배경 레이어 보이기(속도:0.5초)
}

 

/* 레이어 닫기 */
function layer_pop_close() {
 if($('.opacity_bg_layer').length) { // 불투명 배경 레이어가 실행된 상태에서만 진행
  // 불투명 배경 레이어 삭제(속도:0.5초)
  var oj = $('.opacity_bg_layer');
  oj.fadeOut(500, function() {
   oj.remove();
  });
 }
}

 

// 브라우저 창 크기 변경에 따른 처리
$(window).resize(function() {
 if($('.opacity_bg_layer').length) { // 불투명 배경 레이어가 실행된 상태에서만 진행
  opacity_bg_layer();
 }
});

 

/* 화면을 불러온 후 처리 */
$(document).ready(function() {
 opacity_bg_layer();

 $(document).on('click', '.opacity_bg_layer', function() { // 불투명 배경 레이어를 클릭하면 닫기
  layer_pop_close();
 }).keyup(function(e) { // esc 키 사용하면 불투명 배경 레이어 닫기
  if(e.keyCode == 27) layer_pop_close();
 }).on('click', '.opacity_bg_layer_show', function() { // "배경용 불투명 레이어 띄우기"를 클릭하면 불투명 배경 레이어 보이기
  opacity_bg_layer();
 });
});
</script>
</head>
<body>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <span><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></span>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="#" class="opacity_bg_layer_show">배경용 불투명 레이어 띄우기</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>
 <div><a href="http://okkks.tistory.com" target="_blank">okkks.tistory.com</a></div>

</body>
</html>


 

더보기>

- jquery 레이어 팝업창 만들기 / 레이어 가운데 정렬 / div 정중앙 위치 / layer popup center 띄우기

- jquery 모달 팝업창 만들기 / javascript modal popup layer / 모달 레이어 구현 / div modal 팝업창 정 가운데 띄우기 / modal dialog window

Posted by 은둔고수

json 문자열 post 방식으로 전송하기

 

[javascript]

var js = ‘{“a”:1, ”b”:2, “c”:3}’;  // json 문자열 생성하기

js = $.parse(js);  // JSON.parse를 사용하여 문자열을 배열로 변환

$.post(

'/am/lt/lt_ext_cnt_aj.html'

,{“js":js}

,function(data) {

var rtn_json = $.parseJSON(data);  // 데이터를 JSON으로 파싱

if(rtn_json['result']) {

alert(rtn_json[‘data']);

} else {

alert(‘');

}

}

);

 

 

[php]

<?php

$rtn = array();

$rtn['result'] = true;

$rtn[data'] = $_POST[‘js’][‘a’];

echo json_encode($rtn);

exit;

?>

결과 : 1



 

더보기>

- json 객체를 post 전송하기

- javascript 배열을 post 방식으로 값을 넘기기

Posted by 은둔고수

json 객체를 post 전송하기

 

[javascript]

var js = {“a”:1, ”b”:2, “c”:3};  // json 객체 생성하기

$.post(

'주소'

,{“js":js}

,function(data) {

var rtn_json = $.parseJSON(data);  // 데이터를 JSON으로 파싱

if(rtn_json['result']) {

alert(rtn_json[‘data']);

} else {

alert(‘');

}

}

);

 

 

[php]

<?php

$rtn = array();

$rtn['result'] = true;

$rtn[data'] = $_POST[‘js’][‘a’];

echo json_encode($rtn);

exit;

?>

결과 : 1





Posted by 은둔고수

javascript 배열을 post 방식으로 값을 넘기기

 

[javascript]

var ary = [1,2,3];  // 배열 생성하기

/* 또는

var ary = new Array();

ary[0] = 1;

ary[1] = 2;

ary[2] = 3;

 

//jQuery.ajaxSettings.traditional = true;

//$.ajaxSettings.traditional = true;

 

$.post(

'주소'

,{“ary":ary}

,function(data) {

var rtn_json = $.parseJSON(data);  // 데이터를 JSON으로 파싱

if(rtn_json['result']) {

alert(rtn_json[‘data']);

} else {

alert(‘');

}

}

);

 

 

[php]

<?php

$rtn = array();

$rtn['result'] = true;

$rtn[data'] = $_POST[‘ary’][0];

echo json_encode($rtn);

exit;

?>

 

 

결과 : 1

 

 

참고>

jQuery.ajaxSettings.traditional = true;

또는

$.ajaxSettings.traditional = true;

를 사용하는 경우에는 결과가 3이 된다. [jquery ver. 2.1.3]

 

 

참고> 배열을 생성할 때 키 값을 임으로 만드는 경우 위 방식으로 사용할 수 없다.

>

var ary = new Array();

ary[‘a’] = 1;

ary[‘b’] = 2;

ary[‘c’] = 3;




더보기>

- json 객체를 post 전송하기

- json 문자열 post 방식으로 전송하기


Posted by 은둔고수

<img src="없는 이미지" alt="No image!" />

 

 

 

 

// 이미지 오류가 발생할 때 replaceWith를 이용해서 바꿔 출력하기

$(document).ready(function() {
    $("img").error(function() {
        $("img").replaceWith("<p>No image!</p>");
    });

});

 

 

 

 

 

 

// 이미지 태그 엑박이 발생할 때 다른 이미지로 교체하기

$(document).ready(function() {
    $("img").error(function() {
        $("img").attr("src", "noimage.gif");
    });

});

Posted by 은둔고수

jquery를 이용해서 스크롤을 지정한 위치까지 내리면 위로 버튼을 하단 오른쪽에 보여주고 클릭하면 화면 제일 상단으로 이동한다.

모바일웹에서도 사용할 수 있다.

 

 

예> html

<!-- 버튼 : 위로 -->
<button type="button" class="btn_up_layer">위로</button>

 

 

예> css

/* 버튼 : 위로 */
.btn_up_layer {position:absolute;top:0;right:30px;display:none;padding:5px 10px;z-index:1;}

 

 

예> javascript + jquery

/* 위로
스크롤이 특정 위치로 이동하면 위로버튼이 나타난다.
위로버튼을 클릭하면 상단으로 이동
*/
function btn_mv_up(oj) {
 if(!oj) return false;

 var st = $(window).scrollTop();
 var h = $(window).height();

 $(oj).stop().hide().css('top',h + st - 50);    // 스크롤 이동에 따른 위로버튼의 위치 이동
 if(st > 900) { $(oj).fadeIn(); }    // 위로버튼을 보여주는 위치 지정
 else if(st < 900) { $(oj).stop().fadeOut(); }    // 위로버튼을 숨기는 위치 지정

}

 

// 위로 버튼
 $(document).scroll(function() {
  btn_mv_up('.btn_up_layer');
 }).on('click', '.btn_up_layer', function() {
  $("html, body").animate({scrollTop:0}, 'slow');
 });

 

 



더보기>

- jquery 스크롤(scroll) 따라다니는 배너 레이어

Posted by 은둔고수

jquery를 이용해서 스크롤을 지정한 위치까지 내리면 위로 버튼을 보여주고 클릭하면 화면 제일 상단으로 이동한다.

 

 

예> html

<!-- 버튼 : 위로 -->
<button type="button" class="btn_up_layer">위로</button>

 

 

예> css

/* 버튼 : 위로 */
.btn_up_layer {position:fixed;right:15px;bottom:20px;display:none;padding:5px 10px;z-index:1;}

 

 

예> javascript + jquery

/* 위로
스크롤이 특정 위치로 이동하면 위로버튼이 나타난다.
위로버튼을 클릭하면 상단으로 이동
*/
function btn_mv_up(oj) {
 if(!oj) return false;
 var o = $(oj);
 var p = $(window).scrollTop();
 if(p > 300){ o.fadeIn('slow'); }    // 위로버튼이 나타나는 위치 지정
 else if(p < 300){ o.fadeOut('slow'); }    // 위로버튼을 숨기는 위치 지정
}

 

// 위로 버튼
$(document).scroll(function() {
  btn_mv_up('.btn_up_layer');
 }).on('click', '.btn_up_layer', function() {
  $("html, body").animate({scrollTop:0}, 'slow');
});

 

 [스크롤이 상단에 있을 경우는 위로 버튼이 보이지 않는다.]

[스크롤을 지정 위치까지 내리면 위로 버튼이 나타난다.]



더보기>

- jquery scroll 따라다니는 위로 버튼

Posted by 은둔고수

jquery div, table 등의 태그를 동적 생성을 하게 되면 click 이벤트 등이 반응을 하지 않게 되는데

이런 경우 on을 이용해서 처리할 수 있다.

 

예> html

<div class="dom">
 <ul>
  <li>http://okkks.tistory.com/1065 [기본]</li>
 </ul>
 <button type="button" class="add">추가</button>
</div>

 

예> javascript + jquery

<script type="text/javascript">

$('.dom .add').click(function() {
 $('.dom ul').append('<li>http://okkks.tistory.com/1065 [추가]</li>');
});

 

/* 기존 방식 */

/*
$('.dom ul li').click(function() {
 alert($(this).text());
});
*/

 

/* 동적 방식 */

$(document).on('click', '.dom li', function() {
 alert($(this).text());
});

</script>

 

 

 [기존 방식으로 하는 경우 추가된 부분은 click 이벤트가 반응을 하지 않는다.]

 

 

[동적 방식으로 처리하는 경우 추가된 부분도 click 이벤트가 발생한다.]

Posted by 은둔고수