¾È±Ô °øºÎ¹æ

Javascript & JQuery & CSS > load() Ajax ¸Þ¼­µå

µî·ÏÀÏ : 2019-05-27 14:37 Á¶È¸¼ö : 53,368

jQuery load() ¸Þ¼­µå´Â °£´ÜÇÏÁö¸¸ °­·ÂÇÑ AJAX ¸Þ¼­µåÀÔ´Ï´Ù.
¼­¹öÀÇ Æ¯Á¤ÇÑ URLÀÇ µ¥ÀÌÅ͸¦ ºÒ·¯¿Í ¼±ÅÃµÈ ¿ä¼Ò¿¡ ¹ÝȯµÈ µ¥ÀÌÅ͸¦ »Ñ·ÁÁÙ ¼ö ÀÖ½À´Ï´Ù.

Syntax:
$(selector).load(URL,data,callback);

Çʼö URL ¸Å°³ º¯¼ö´Â·ÎµåÇÏ·Á´Â URLÀ» ÁöÁ¤ÇÕ´Ï´Ù.
¼±ÅÃÀû data ¸Å°³ º¯¼ö´Â ¿äû°ú ÇÔ²² Àü¼ÛÇÒ Äõ¸® ¹®ÀÚ¿­ Å° / °ª ½Ö ¼¼Æ®¸¦ ÁöÁ¤ÇÕ´Ï´Ù.
¼±ÅÃÀû ÄÝ¹é ¸Å°³ º¯¼ö´Â load () ¸Þ¼­µå°¡ ¿Ï·áµÈ ÈÄ ½ÇÇàÇÒ ÇÔ¼öÀÇ À̸§ÀÔ´Ï´Ù .

¿¹Á¦ ÆÄÀÏÀÇ ³»¿ëÀº ´ÙÀ½°ú °°½À´Ï´Ù. "demo_test.txt":

< h2 > jQuery¿Í AJAX´Â Àç¹Ì ÀÖ½À´Ï´Ù !!! < / h2 >
< p id = "p1"> ´Ü¶ôÀÇ ÀϺΠÅؽºÆ®ÀÔ´Ï´Ù. < / p >
´ÙÀ½ ¿¹Á¦¿¡¼­´Â "demo_test.txt"ÆÄÀÏÀÇ ³»¿ëÀ» ƯÁ¤ <div> ¿ä¼Ò¿¡·ÎµåÇÕ´Ï´Ù.

¿¹
$ ( "#div1" ) .load ("demo_test.txt" );

URL ¸Å°³ º¯¼ö¿¡ jQuery ¼±Åñ⸦ Ãß°¡ ÇÒ ¼öµµ ÀÖ½À´Ï´Ù.
´ÙÀ½ ¿¹Á¦¿¡¼­´Â "demo_test.txt"ÆÄÀÏ ¾È¿¡ id = "p1"ÀÎ ¿ä¼ÒÀÇ ³»¿ëÀ» ƯÁ¤ <div> ¿ä¼Ò·Î ·ÎµåÇÕ´Ï´Ù.

¿¹
$ ("# div1" ) .load ( "demo_test.txt #p1" );

¼±ÅÃÀû callback ¸Å°³ º¯¼ö´Â load () ¸Þ¼­µå°¡ ¿Ï·á µÉ ¶§ ½ÇÇàÇÒ Äݹé ÇÔ¼ö¸¦ ÁöÁ¤ÇÕ´Ï´Ù .
Äݹé ÇÔ¼ö´Â ´Ù¸¥ ¸Å°³ º¯¼ö¸¦ °¡Áú ¼ö ÀÖ½À´Ï´Ù.

responseTxt - È£ÃâÀÌ ¼º°øÇÏ¸é °á°ú ÄÁÅÙÆ®¸¦ Æ÷ÇÔÇÕ´Ï´Ù.
statusTxt - È£Ãâ »óŸ¦ Æ÷ÇÔÇÕ´Ï´Ù.
xhr - XMLHttpRequest °´Ã¼¸¦ Æ÷ÇÔÇÕ´Ï´Ù.

´ÙÀ½ ¿¹Á¦¿¡¼­´Â load () ¸Þ¼­µå°¡ ¿Ï·áµÈ ÈÄ °æ°í »óÀÚ¸¦ Ç¥½ÃÇÕ´Ï´Ù. »ó±â Áß°£ ºÎÇÏ () ¸Þ¼Òµå°¡ ¼º°ø, ±×°ÍÀº! "¿ÜºÎ ÄÜÅÙÃ÷°¡ ¼º°øÀûÀ¸·Î·Îµå"Ç¥½ÃÇÏ°í ½ÇÆÐ ÇÒ °æ¿ì ¿À·ù ¸Þ½ÃÁö°¡ Ç¥½ÃµË´Ï´Ù
 

$("button").click(function(){
  $("#div1").load("demo_test.txt", function(responseTxt, statusTxt, xhr){
    if(statusTxt == "success")
      alert("¿ÜºÎ ÄÜÅÙÃ÷¸¦ ¼º°øÀûÀ¸·Î·ÎµåÇß½À´Ï´Ù!");
    if(statusTxt == "error")
      alert("Error: " + xhr.status + ": " + xhr.statusText);
  });
});


¾Æ·¡ ¿¹Á¦´Â load() ¸Þ¼­µå¸¦ ÀÌ¿ëÇÑ °£´ÜÇÑ ÀÀ¿ë ¿¹Á¦ÀÔ´Ï´Ù.
 

<script type="text/JavaScript">
$(document).ready(function() {

	 $("*[obj-type=loadPage]").on("click", function() {

            var page_info = $(this).attr('page-info');

            layerLoad(page_info);
	 });
});


function layerLoad(page)
{
    var page_info = $.parseJSON( page.replace(/\'/g, "\"") );

    console.info('target:' + page_info.target);
	param_info	= "page=2";

    $("#"+page_info.target).load(page_info.view, param_info, function(res, status, xhr){

        if( status == "error")
        {
            alert("ÆäÀÌÁö ·Îµù¿¡ ½ÇÆÐÇß½À´Ï´Ù.\n");
            return;
        }

    });
}


</script>

<div>
<ul>
	<li obj-type="loadPage" page-info="{'type':'left11', 'view':'/test/test1.asp','target':'aaa'}">¸Þ´º1</li>
	<li obj-type="loadPage" page-info="{'type':'left22', 'view':'/test/test2.asp','target':'bbb'}">¸Þ´º2</li>
	<li obj-type="loadPage" page-info="{'type':'left33', 'view':'/test/test3.asp','target':'ccc'}">¸Þ´º3</li>
</ul>
</div>

<div id="aaa"></div>
<div id="bbb"></div>
<div id="ccc"></div>




 

 




 

 
¡Ø Ȥ½Ã µµ¿òÀÌ µÇ¼Ì´Ù¸é ´ñ±Û¿¡ ÇѸ¶µð ³²°ÜÁÖ¼¼¿ä!
ÀÛ¼ºÀÚ   ºñ¹Ð¹øÈ£
ÀÚµ¿±Û ¹æÁö     (ÀÚµ¿±Û ¹æÁö ±â´ÉÀÔ´Ï´Ù.)
³»¿ë   ´ñ±Û´Þ±â 
À̸ÞÀÏ ¹®ÀÇ : cak0280@nate.com  
Copyright 2000 By ENTERSOFT.KR All Rights Reserved.