23.05.16

์ด์ค€์˜ยท2023๋…„ 5์›” 16์ผ
0
post-thumbnail
jQuery

javascript - library
dom ๊ด€๋ จ๋œ ๋‚ด์šฉ ์ฒ˜๋ฆฌ

https://api.jquery.com/   or   https://www.w3schools.com/jquery/jquery_selectors.asp

์‚ฌ์ดํŠธ ์ฐธ์กฐ

1. selector <-- css
2. ์ฒ˜๋ฆฌ์šฉ ๋ฉ”์„œ๋“œ
		1 . ๋ฐฐ์—ด
        2. 

๐ŸŒ• jQuery

for.each๋ง๊ณ ๋„ map()- ๋‚ด์šฉ๋ณ€๊ฒฝ / filter() - ๋‚ด์šฉ๊ฒ€์ƒ‰ ๋“ฑ์˜ ๊ธฐ๋Šฅ์ด ๋” ์žˆ๋‹ค.

๐ŸŒ™ filter() : ๋‚ด์šฉ ๊ฒ€์ƒ‰

๋‚ด์šฉ์„ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ ์šฉ์‹œ์ผœ์ค€๋‹ค.

1.
	$('h2').filter(':even').css('color', 'coral'); <-- h2 ์ค‘ ์ง์ˆ˜๋ฒˆ ์ฐพ์•„์„œ ์ƒ‰ ๋ณ€๊ฒฝ

2.
    //$('h2').filter(':even').css({
			
		//color: 'coral',
		//backgroundColor : 'yellow'   <-- ๊ฐ์ฒดํ˜•์‹์œผ๋กœ ๋ฐฐ๊ฒฝ๊ณผ ํ•จ๊ป˜ ์ค˜๋„ ๋จ
		//});
  
3.  
   //const h2 = $('h2');  //htmlcollection, ๋ฐฐ์—ด๊ณผ ๋น„์Šท
		//console.log(h2);
		//h2.filler(':even').css('color', 'coral');    <--- ๋ณ€์ˆ˜  ์„ ์–ธ ํ›„ ์ ๊ธฐ
    
    
----------------------    
<h2>Header - 1</h2>
<h2>Header - 2</h2>
<h2>Header - 3</h2>
<h2>Header - 4</h2>
<h2>Header - 5</h2>

๐ŸŒ™ filter() ์•ˆ์— ํ•จ์ˆ˜ ์ฃผ๊ธฐ

<script type="text/javascript">
	$( document ).ready( function()  {
		$('h2').filter( function() {
			console.log('h2');
		});
				
	});
</script>

each์™€ ๋˜‘๊ฐ™์ด ๋‚ด๋ถ€์ ์œผ๋กœ ๋ฃจํ”„๋ฅผ ๋Œ๊ณ  ์žˆ๋‹ค๊ณ  ๋ณด๋ฉด ๋œ๋‹ค.


$( document ).ready( function()  {
		$('h2').filter( function(index) {
        	// true / false
			return index % 2 == 0;   <--- index๊ฐ€ ์ง์ˆ˜์ผ ๋•Œ
		}).css('color', 'coral');  <--- ์ƒ‰ ๋ณ€๊ฒฝํ•˜๊ฒ ๋‹ค.
				
	});
    
    ๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค๊ฐ€ ์ง์ˆ˜์ธ ๊ฒƒ๋งŒ ์ƒ‰ ๋ณ€๊ฒฝํ•˜๋Š” 'ํ•„ํ„ฐ์˜ ๋‚ด์šฉ์„ ๋งŒ๋“ค์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.'



๐ŸŒ™ end() : filter ์ ์šฉ ์ทจ์†Œ


		$('h2').css('background-color', 'lightcoral');	
		$('h2').filter(':even').css('color', 'blue');	
		$('h2').filter(':odd').css('color', 'white');


		$('h2').css('background-color', 'lightcoral')
		.filter(':even').css('color', 'blue')
		.filter(':odd').css('color', 'white');  //even ์ค‘์˜ odd๊ฐ€ ์ ์šฉ๋˜๋Š” ๊ฒƒ์ž„

.์„ ์ด์šฉํ•˜์—ฌ ํ•œ ๋ฒˆ์— ์—ฐ๊ฒฐํ•ด์„œ ์œ„์™€ ๊ฐ™์ด ์“ฐ๋ ค๊ณ  ํ–ˆ์œผ๋‚˜ odd๊ฐ€ filter์˜ filter๋กœ ์ ์šฉ๋˜์–ด even ์ค‘์—์„œ odd๊ฐ€ ๋ณ€๊ฒฝ๋˜๋„๋ก ์ ์šฉ์ด ๋จ(1, 3, 5 ์ค‘์˜ ์ง์ˆ˜๋Š” 2๋ฒˆ์งธ์ธ 3)


		$('h2').css('background-color', 'lightcoral')
		.filter(':even').css('color', 'blue')
		.end()   //filter ์ ์šฉ๋œ ๊ฒƒ์ด ์ทจ์†Œ๋˜์–ด ๋ฐ‘ ํ•„ํ„ฐ๊ฐ€ ๋งจ ์ฒ˜์Œ ๊ฒƒ์— ๋งž๊ฒŒ ์ ์šฉ
		.filter(':odd').css('color', 'white');

end๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ filter ์ ์šฉ์„ ์ทจ์†Œ ์‹œ์ผœ ๊ทธ ์ดํ›„์— ๋‚˜์˜ค๋Š” filter๊ฐ€ ๋งจ ์œ„์—์„œ filter ํ•˜๋„๋ก ์ ์šฉ์‹œ์ผœ์ค€๋‹ค. ( ๊ฒฐ๊ณผ ์ฒ˜์Œ๊ณผ ๋™์ผ )



๐ŸŒ™ add : ๋’ค์— ์ถ”๊ฐ€ํ•˜์—ฌ ์ ์šฉ

//$('h1').css('background-color', 'coral');
//$('h1, h2').css('color', 'blue');


$('h1').css('background-color', 'coral')
.add('h2').css('color', 'blue');    (.add๋กœ h1์—์„œ ์ถ”๊ฐ€๋กœ h2๋„ ๊ธ€์ž์ƒ‰ ๋ฐ”๊พผ๋‹ค๋Š” ๋œป)

(๊ฒฐ๊ณผ๋Š” ์œ„์™€ ๋™์ผ)

h1 ์ ์šฉ์„ ํฌํ•จํ•ด์„œ h2 ์ ์šฉํ•˜๋Š” ๊ฒƒ



๐ŸŒ™ find : ํŠน์ • ํƒœ๊ทธ ๊ฒ€์ƒ‰

1.
console.log($(document).find('h2')); <-- h2ํƒœ๊ทธ๋ฅผ ๊ฒ€์ƒ‰ํ•ด์คŒ

2.
$(document).find('h2').each(function(index, item) {
			console.log(index, ':', item.innerHTML);   <--- each๋กœ ๋ฃจํ”„๋Œ๋ ค์„œ ๊ฒ€์ƒ‰๋œ ๊ฒฐ๊ณผ์˜ ๋‚ด์šฉ๋งŒ ๊ฐ€์ ธ์˜ด

3.
$(document).find('.c').each(function(index, item) {  <--- class="c"์ธ ๊ฒƒ ๊ฒ€์ƒ‰
			console.log(index, ':', this.innerHTML);
		});

๐ŸŒ™๐ŸŒ™ find : String - xml ๋ณ€๊ฒฝํ•˜๊ธฐ (xml ๋ถ„์„)

xml ํ˜•์‹ ๋ณ€์ˆ˜ ํ•˜๋‚˜ ์ ๋‹นํ•˜๊ฒŒ ๋งŒ๋“ค๊ธฐ

const xml = `<friends>
	<friend>
		<name>tester1</name>
		<language>Javascript</language>
	</friend>
	<friend>
		<name>tester2</name>
		<language>Java</language>
	</friend>
	<friend>
		<name>tester3</name>
		<language>Python</language>
	</friend>
	</friends>
    `;    
    
    ํ˜„์žฌ๋Š” string ํ˜•์‹์ด๋‹ค.

ํƒ€์ž… ๋ณ€๊ฒฝ

	$( document ).ready( function()  {
		console.log(typeof xml);
		
		const xmlDoc = $.parseXML(xml); // xml Domํ™” ์‹œํ‚ด
		console.log(typeof xmlDoc);  
	});    
    
    string - > object๋กœ ๋ฐ”๊ฟˆ


find ์ ์šฉ


$(xmlDoc).find('friend').each(function(index, item) {
			console.log(index);
			console.log($(item).find('name')[0].innerHTML); <--name๋งŒ ๋ฝ‘๊ธฐ
			});
            




๐ŸŒ™ ์กด์žฌ ์—ฌ๋ถ€ ๊ฒ€์ƒ‰


$('h2').each(function() {
			// is : ์•ˆ์— selector ์กด์žฌ ์—ฌ๋ถ€ ๊ฒ€์ƒ‰
			if($(this).is('.c')) {
				$(this).css('color', 'coral');
			}
		});



๐ŸŒ™ ๋™์  ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ(json)

	$( document ).ready( function()  {
		// json 
		let object = {name: "ํ™๊ธธ๋™"};
		console.log(object);
		
    1.     
		//๋™์ ์œผ๋กœ ํ”„๋กœํผํ‹ฐ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ
		object.region1 = '์„œ์šธ์‹œ ์ข…๋กœ๊ตฌ';
		object.part1 = '๋ฆฌ๋”';
		
		console.log(object);
		
	});
    
    2. ๊ฐ์ฒด์— ๋Œ€ํ•œ ํ™•์žฅ (์œ„์™€ ๋™์ผํ•˜๊ฒŒ ๊ฐ์ฒด๊ฐ€ ์ถ”๊ฐ€๋จ)
   	 	$.extend(object, {region2: '์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ', part2: '๋ฒ ์ด์Šค'});
		$.extend(object, {region3: '์„œ์šธ์‹œ ์„œ์ดˆ๊ตฌ', part3: '์„ธ์ปจ๋“œ'});
		console.log(object);
    



๐ŸŒ™ $ ๋ณ€๊ฒฝํ•˜๊ธฐ

$.noConflict(); <-- $ ์ถฉ๋Œ๋จ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์‚ฌ์šฉ ์•ˆํ•œ๋‹ค๋Š” ๋œป

	$.noConflict();
	const J = jQuery;
	//$( document ).ready( function()  {
	J( document ).ready( function()  {

		console.log("Hello jQuery");

	});


์œ„ extend ์ฝ”๋“œ ๋ณ€๊ฒฝํ•ด๋ณด๊ธฐ

$.noConflict();
	const J = jQuery;
	//$( document ).ready( function()  {
	J( document ).ready( function()  {

		let object = {name: "ํ™๊ธธ๋™"};
		
		J.extend(object, {region2: '์„œ์šธ์‹œ ๊ฐ•๋‚จ๊ตฌ', part2: '๋ฒ ์ด์Šค'});
		J.extend(object, {region3: '์„œ์šธ์‹œ ์„œ์ดˆ๊ตฌ', part3: '์„ธ์ปจ๋“œ'});
		console.log(object);

	});



๐ŸŒ™ ๋ฌธ์„œ ๋‚ด์šฉ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ๋ณ€๊ฒฝํ•  ๋•Œ ์‚ฌ์šฉ

๊ธฐ๋ณธ์ ์œผ๋กœ ์‚ฌ์šฉ		๋ณ€๊ฒฝ
innerHTML		==> html();
textContent		==> text();

1. ๋‚ด์šฉ ํ™•์ธ ๋ฒ„ํŠผ ๋ˆ„๋ฅผ ์‹œ

$( document ).ready( function()  {
		//innerHTML		==> html();
		//textContent	==> text();
		document.getElementById('btn1').onclick = function() {
        
			//javascript ๋ฐฉ์‹
			const html = document.getElementById('result');
			console.log(html.innerHTML);
			console.log(html.textContent);
		};
	}); 
    
			// jQuery ๋ฐฉ์‹
			console.log($('#result').html());
			console.log($('#result').text());
            
-------------------------------------------------------------

2. ๋‚ด์šฉ ์ˆ˜์ • ๋ฒ„ํŠผ ๋ˆ„๋ฅผ ์‹œ
		document.getElementById('btn2').onclick = function() {
			//javascript ๋ฐฉ์‹
			//const html = document.getElementById('result');
			//html.innerHTML = '<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ</i>';
			//html.textContent = '<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ</i>';
			
			// jQuery
			console.log($('#result').html('<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ</i>'));
			//console.log($('#result').text('<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ</i>');
		};
            
            
--------------------------------------------------------------       
<body>

<button id="btn1">๋‚ด์šฉ ํ™•์ธ</button>
<button id="btn2">๋‚ด์šฉ ์ˆ˜์ •</button>
<br><hr><br>
<div id="result"><b>์ถœ๋ ฅ ๋‚ด์šฉ</b></div>

</body>



๐ŸŒ™ ์ง‘ํ•ฉ์  / ๋ถ„๋ฆฌ์  ์ ์šฉ

<script type="text/javascript">
	$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
			
            // 1. ์ง‘ํ•ฉ์  ์ ์šฉ
			//$('div').html('<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ</i>');
			
			//2. ๋ถ„๋ฆฌ์ 
			$('div').html( function(index) {
				return '<i>์ƒˆ๋กœ์šด ๊ฒฐ๊ณผ ' + index + '</i>'
			})
		};

	});
</script>

โญ ์‘์šฉ : ๊ตฌ๊ตฌ๋‹จ

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script type="text/javascript">
	$( document ).ready( function()  {
		document.getElementById('btn').onclick = function() {
			const sDan = $('#startdan').val();
			const eDan = $('#enddan').val();

			const iStartDan = parseInt(sDan);
			const iEndDan = parseInt(eDan);
			
				let html = '<table border="1" width="800">';
				for(let i = iStartDan; i <=iEndDan; i++) {
					html += '<tr>';
					for(let j = 1; j <=9; j++) {
					html += '<td>' + i + 'x' + j + '=' + (i*j) + '</td>';
					}
					html += '</tr>';					
				}
				html += '</table>';
				
				$('#result').html( html );
			};	
		});
</script>
</head>
<body>

์‹œ์ž‘๋‹จ <input type="text" id="startdan" />
~
๋๋‹จ <input type="text" id="enddan" />
<button id="btn">๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅ</button>
<br><hr><br>
<div id="result"></div>

</body>


$(document).ready(function() {
	document.getElementById('btn').onclick = function() {
		// $('ํƒœ๊ทธ') - selector
		// $('html ๋‚ด์šฉ') - ์ƒ์„ฑ
		
		// appendTo ๋ถ™์ด๋Š” ๊ฒƒ. / prependTo / insertAfter / insertBefore
		$( '<i>Hello jQuery</i>' ).appendTo('#result');
		$('<i></i>').html('Hello jQuery').prependTo('#result');
	} 
});
  1. appendTo : ๋’ค์— ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ
  2. perpendTo : ์•ž์— ๋ถ™์—ฌ์„œ ์ถœ๋ ฅ
  3. insertAfter :
  4. insertBefore :

๐ŸŒ™ ์‚ญ์ œํ•˜๊ธฐ

<script type="text/javascript">
	$( document ).ready( function()  {
		1.
        //๋‚ด์šฉ์‚ญ์ œ
		document.getElementById('btn1').onclick = function() {
			$('#d').html('');
		};
        
		2.
		//๋‚ด์šฉ์‚ญ์ œ
		document.getElementById('btn2').onclick = function() {
			$('#d').empty();
		};
		
        3.
		//ํƒœ๊ทธ์‚ญ์ œ
		document.getElementById('btn3').onclick = function() {
			$('#d').remove(); //divํƒœ๊ทธ ์ „์ฒด๊ฐ€ ์‚ญ์ œ๋จ
		};
		
        4.
		document.getElementById('btn4').onclick = function() {
			$('P').first().remove();  //์ฒซ๋ฒˆ์งธ ๊ฒƒ๋งŒ ์ง€์›€
		};


	});
</script>

---------------------------------------------------------------

</head>
<body>

<button id="btn1">๋‚ด์šฉ ์‚ญ์ œ</button>
<button id="btn2">๋‚ด์šฉ ์‚ญ์ œ</button>
<button id="btn3">๋‚ด์šฉ ์‚ญ์ œ</button>
<button id="btn4">๋‚ด์šฉ ์‚ญ์ œ</button>
<br><hr><br>

<div id="d">
	<p>๋‚ด์šฉ 1</p>
	<p>๋‚ด์šฉ 2</p>
</div>
</body>

1,2.



๐ŸŒ™ ์ด๋ฏธ์ง€ ๋„ฃ๊ธฐ, ์ •๋ณด ์ˆ˜์ •, ๊ฐ€์ ธ์˜ค๊ธฐ

์ด๋ฏธ์ง€ ๋„ฃ๊ณ  ์ด๋ฏธ์ง€์˜ ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ


<script type="text/javascript">
	$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
			//1๊ฐœ๋งŒ ์ถœ๋ ฅ๋จ
			//console.log($('img').attr('src'));
			
            1.
			$('img').each( function(index, item) {
				console.log($(item).attr('src'));
			});   // attr -> attribute
			
            2.
			$('img').attr('src', function(index, item) {
				console.log(item);
			})
		}
	});
</script>

1,2 ๊ฒฐ๊ณผ๋Š” ๋™์ผ

-------------------------------------------------------
<body>

<button id="btn1">์†์„ฑ ๋‚ด์šฉ</button>
<br><hr><br>
<img src="./images1/Chrysanthemum.jpg" width="150" />
<img src="./images1/Desert.jpg" width="150" />
<img src="./images1/Hydrangeas.jpg" width="150" />
<img src="./images1/Jellyfish.jpg" width="150" />

</body>


์ด๋ฏธ์ง€ ์ˆ˜์ •ํ•˜๊ธฐ

1. ์•„๊นŒ์™€ ๋‹ค๋ฅด๊ฒŒ ์ „์ฒด ๋‹ค ์ ์šฉ๋œ๋‹ค.
document.getElementById('btn2').onclick = function() {
			$('img').attr('width', 100);
		}


2. ์ „์ฒด ๋‹ค ์ ์šฉ + ๊ฐ์ž ๋‹ค๋ฅธ ํฌ๊ธฐ(ํฌ๊ธฐ๊ฐ€ 100์”ฉ ์ฆ๊ฐ€ํ•จ)
document.getElementById('btn2').onclick = function() {
			$('img').attr('width', function(index) {
				return (index + 1) * 100;
			});
        
        
3. width + height ์ฃผ๊ธฐ
document.getElementById('btn2').onclick = function() {
			$('img').attr( {
				width: 100,
				height: 200
			});
		};
        

4. 2๋ฒˆ ํ™•์žฅ
document.getElementById('btn2').onclick = function() {
			$('img').attr( {
				width: function(index) {
					return (index + 1) * 100;
				},
				height: 200
			});
		};

์†์„ฑ ์‚ญ์ œ

document.getElementById('btn3').onclick = function() {
			$('img').removeAttr('height');     <--- ์ฃผ์–ด์ง„ ๋†’์ด ๊ธธ์ด ์‚ญ์ œ
		}

์†์„ฑ ๋งŒ๋“ค์–ด์„œ ์‚ฌ์šฉ

src, id ๋“ฑ ๊ธฐ์กด ์†์„ฑ ๋ง๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค์–ด์„œ ๋Œ€์ž…ํ•  ์ˆ˜ ์žˆ๋‹ค.

<img src="./images1/Chrysanthemum.jpg" width="150" data="1" />
<img src="./images1/Desert.jpg" width="150" data="2" />
<img src="./images1/Hydrangeas.jpg" width="150" data="3"/>
<img src="./images1/Jellyfish.jpg" width="150" data="4"/>   <-- data๋ผ๋Š” ์†์„ฑ ๊ฐ๊ฐ ๋ถ€์—ฌ

$('img').each( function(index, item) {
				console.log($(item).attr('data')); <--- ๋งŒ๋“  ์†์„ฑ์œผ๋กœ ์ถœ๋ ฅ
			});




๐ŸŒ™ ์†์„ฑ์œผ๋กœ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ

ํ…์ŠคํŠธ

$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
        1. ๊ธฐ๋ณธ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ
			//console.log(document.frm.data.value);
		
        2.	jQuery
            console.log($(#data).val());

		3. ๋ฐ์ดํ„ฐ๊ฐ’ ๋„ฃ๊ธฐ
        
        document.getElementById('btn2').onclick = function() {
			$('#data').val('์ƒˆ๋กœ์šด ๋ฐ์ดํ„ฐ');
			
		};
	
	});
    
 ------------------------------------------------------------------------
 
 <body>

<button id="btn1">๋‚ด์šฉ</button>
<button id="btn2">๋‚ด์šฉ ์ถ”๊ฐ€</button>
<br><hr><br>
<form name="frm">
๋ฐ์ดํ„ฐ <input type="text" name="data" id="data" />
</form>

</body>

1,2.


์ฒดํฌ๋ฐ•์Šค / select

์ฒดํฌ๋ฐ•์Šค


	$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
			//console.log(document.frm.data.value);
			$('input:checkbox:checked').each( function() {
				
			console.log($(this).val());
			});
			
		};	
	
	});

-------------------------------------------------------------

<body>

<button id="btn1">๋‚ด์šฉ</button>
<br><hr><br>
<form name="frm">
	<input type="checkbox" id="ch1" name="ch" value="์ˆ˜๋ฐ•">์ˆ˜๋ฐ•<br>
	<input type="checkbox" id="ch2" name="ch" value="๋”ธ๊ธฐ">๋”ธ๊ธฐ<br>
	<input type="checkbox" id="ch3" name="ch" value="์‚ฌ๊ณผ">์‚ฌ๊ณผ<br>
	<input type="checkbox" id="ch4" name="ch" value="์ž๋ชฝ">์ž๋ชฝ<br>
</form>

</body>


select

	$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
			//console.log(document.frm.data.value);
			$('input:checkbox:checked').each( function() {
			
            //checkbox
			console.log($(this).val());
			});
            
            //select
			console.log('select :', $('#sel option:selected').val());
			
		};	
	
	});
</script>
</head>
<body>

<button id="btn1">๋‚ด์šฉ</button>
<br><hr><br>
<form name="frm">
	<input type="checkbox" id="ch1" name="ch" value="์ˆ˜๋ฐ•">์ˆ˜๋ฐ•<br>
	<input type="checkbox" id="ch2" name="ch" value="๋”ธ๊ธฐ">๋”ธ๊ธฐ<br>
	<input type="checkbox" id="ch3" name="ch" value="์‚ฌ๊ณผ">์‚ฌ๊ณผ<br>
	<input type="checkbox" id="ch4" name="ch" value="์ž๋ชฝ">์ž๋ชฝ<br>
	<br>
	
	<select id="sel">
	<option value="์‚ฌ๊ณผ">์‚ฌ๊ณผ</option>
	<option value="์ž๋ชฝ">์ž๋ชฝ</option>
	<option value="๋”ธ๊ธฐ">๋”ธ๊ธฐ</option>
	<option value="๋ฉœ๋ก ">๋ฉœ๋ก </option>
	</select>
</form>



css ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ

<style type="text/css">
	.c1 { color: coral; }
	.c2 { color: pink; }
	.c3 { color: red; }
</style>

-----------------------------

<script type="text/javascript">
	$( document ).ready( function()  {
		document.getElementById('btn1').onclick = function() {
        1. ํ•˜๋‚˜์˜ ์ •๋ณด๋งŒ ๊ฐ€์ ธ์˜ด
			console.log($('h2').css('color')); //css ์„ค์ •ํ•œ color์ž„
		
        2.	๋‹ค ๊ฐ€์ ธ์˜ด(each)
			$('h2').css('color', function(index, item) {
				console.log(item);
			})
		}
	
	});
</script>

-----------------------------

<body>
<button id="btn1">๋‚ด์šฉ</button>
<br><hr><br>

<h2 class="c1">header-1</h2>
<h2 class="c2">header-2</h2>
<h2 class="c3">header-3</h2>
</body>

๐ŸŒ™ ์ƒ‰๊น” ๋ณ€๊ฒฝ

1. ์ „๋ถ€ ๋‹ค ๋ณ€๊ฒฝ
document.getElementById('btn2').onclick = function() {
				$('h2').css('color', 'cyan');
			};
            

2. ๊ฐ๊ฐ ์ƒ‰์„ ์ค˜์„œ ๋ณ€๊ฒฝ
document.getElementById('btn2').onclick = function() {
				//$('h2').css('color', 'cyan');
				const colors = ['purple', 'black', 'aqua']; //๊ฐ๊ฐ ์ˆœ์„œ๋Œ€๋กœ ๋“ค์–ด๊ฐ
				
				$('h2').css('color', function(index){
					return colors[index];
				});
			};



------------------------------------------            
<button id="btn2">๋ณ€๊ฒฝ</button>            



๐ŸŒ™ addClass : ํด๋ž˜์Šค ์ถ”๊ฐ€ / ์‚ญ์ œ

<script type="text/javascript">
		$( document ).ready( function()  {
			1. ํด๋ž˜์Šค ์ถ”๊ฐ€
            document.getElementById('btn1').onclick = function() {
				$('h2').addClass('c');
			};

			2. ํด๋ž˜์Šค ์‚ญ์ œ
			document.getElementById('btn2').onclick = function() {
				$('h2').removeClass('c');

			};
            
            3. ํ† ๊ธ€(๊ป๋‹ค ์ผฐ๋‹ค ํ•˜๊ธฐ)
            document.getElementById('btn2').onclick = function() {
				$('h2').toggleClass('c');

			};
		});
</script>
    
-----------------------------------------------------------------

<body>

<button id="btn1">๋‚ด์šฉ</button>
<button id="btn2">๋ณ€๊ฒฝ</button>
<button id="btn3">ํ† ๊ธ€</button>
<br><hr><br>

<h2 >header-1</h2>
<h2 >header-2</h2>
<h2>header-3</h2>


</body>



๐ŸŒ™ jQuery ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ

์ด๋ฒคํŠธ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•

1.
$('์ด๋ฒคํŠธ ๋Œ€์ƒ').on('์ด๋ฒคํŠธ', function() {
		์ฒ˜๋ฆฌ
});


2.
$('์ด๋ฒคํŠธ ๋Œ€์ƒ').์ด๋ฒคํŠธ(function() {
		์ฒ˜๋ฆฌ
});
1. ๋ฒ„ํŠผ ํด๋ฆญ
$( document ).ready( function()  {
	$('#btn1').click(function() {
		alert('btn1 ํด๋ฆญ');
	});

2. ๋ฒ„ํŠผ์— ๋งˆ์šฐ์Šค๋ฅผ ๋Œˆ์‹œ ์ฒ˜๋ฆฌ๋จ
$('#btn2').mouseover(function() {
		alert('btn2 ์ฒ˜๋ฆฌ');
	});
			
3. ๋ฒ„ํŠผ ํด๋ฆญ2
$('#btn3').on( 'click', function() {
	alert('btn3 ํด๋ฆญ');
});


4.์ด๋ฒคํŠธ ํ†ตํ•ฉ ์ถœ๋ ฅ
$('#btn2').on('click mouseover mouseleave', function() {
		console.log('btn2 ๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ');  <-- ๋ญ˜ ํ•˜๋“  ์ด๊ฒŒ ์ถœ๋ ฅ
});


4-1. ์‚ฌ์šฉํ•œ ์ด๋ฒคํŠธ ๋ณ„๋กœ ๋”ฐ๋กœ ์ฝ˜์†”์—  ์ถœ๋ ฅ
$('#btn2').on({
	'click':function() {
    	console.log('click');
    },
    mouseover :function() {
       	console.log('mouseover');
    },
    mouseleave: :function() {
        console.log('mouseleave');

    },
	}
});
            

1,3.


๋ฒ„ํŠผ ํด๋ฆญ

1. ๊ฐ๊ฐ ํด๋ฆญ๊ฐ’ ๋‹ค๋ฅด๊ฒŒ ํ•˜๊ธฐ
$( document ).ready( function()  {
			$('button').on('click', function() {
				alert('๋ฒ„ํŠผ ํด๋ฆญ : ' + $(this).attr('id'));
			});
			
		});
        
--------------------------------------        
        
<body>
<button id="btn1">๋ฒ„ํŠผ 1</button>
<button id="btn2">๋ฒ„ํŠผ 2</button>
<button id="btn3">๋ฒ„ํŠผ 3</button>
</body>



๐ŸŒ™ ajax ์š”์ฒญํ•˜๊ธฐ

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
	<script type="text/javascript">
		// ready == window.onload
		$( document ).ready( function()  {
			$('#btn1').on('click', function() {
				$.ajax({
					url: './data/csv1.jsp',
					type: 'get',
					dataType: 'text',
					success: function(csvData) {
						console.log('์„ฑ๊ณต', typeof csvData);						
						console.log('์„ฑ๊ณต', csvData);						
					},
					error: function(err) {
						console.log('์‹คํŒจ', err.status);
						console.log('์‹คํŒจ', err.responseText);
					}
				})
			});
		});
	</script>
    
 ------------------------------------------------------------------
 
 <body>
<button id="btn1">์š”์ฒญ</button>
<br><hr><br>
</body>
  1. ์„ฑ๊ณต์‹œ
  1. ์‹คํŒจ์‹œ

๐ŸŒ™ dataType : ~ <- ~์— ๋”ฐ๋ผ ํƒ€์ž…์ด ์ •ํ•ด์ง

dataType: 'xml'

์œ„ ์ฝ”๋“œ์—์„œ url, dataType ๋งŒ ๋ณ€๊ฒฝ

url: './data/xml1.jsp',

dataType: 'xml',

dataType: 'json'


url: './data/json1.jsp',

dataType: 'json',

๐ŸŒ™ ์‘์šฉ xml ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
	<script type="text/javascript">
		// ready == window.onload
		$( document ).ready( function()  {
			$('#btn1').on('click', function() {
				$.ajax({
					url: './data/xml1.jsp',
					type: 'get',
					dataType: 'xml',  //์ด๊ฒƒ์— ๋”ฐ๋ผ ๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋”ฐ๋ผ๊ฐ
					success: function(xmlData) {
						
						let html = '<table border="1" width="800">';
						$(xmlData).find('book').each(function() {
							html += '<tr>';
							html += '<td>' + $(this).find('name').text() + '</td>';
							html += '<td>' + $(this).find('publisher').text() + '</td>';
							html += '<td>' + $(this).find('author').text() + '</td>';
							html += '<td>' + $(this).find('price').text() + '</td>';
							html += '</tr>';	
						});									
							html += '</table>';
							
							$('#result').html(html);
				
					},
					error: function(err) {
						console.log('์‹คํŒจ', err.status);
						console.log('์‹คํŒจ', err.responseText);
					}
				})
			});
		});
	</script>
</head>
<body>


<button id="btn1">์š”์ฒญ</button>
<br><hr><br>

<div id="result"></div>

</body>
</html>

profile
๋„์ ๋„์ 

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