๐ป
JavaScript ์ ์ดํด - [jQuery] DOM ํจ์(1) ๋ณธ๋ฌธ
https://api.jquery.com/category/manipulation/
01.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํจ์</title>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
var arr = new Array();
var strHTML = $('#first').html();
var strText = $('#first > p').html();
arr.push(strHTML);
arr.push(strText);
alert(arr.join("\n"));
var str="<h3>์๋
ํ์ธ์.</h3>";
$('#second').text(str);
$('#third').html(str);
});
</script>
<body>
<div id="first">
์๋
ํ์ธ์. <br/>
<p>
๋ง์ ํ์ ์ ์ด๋ฃฉํ๊ธฐ ์ํ
์ฐ๋ฆฌ ๋ณต์ฑ์ ๋ง๋์ ๋
ธ๋ ฅ์ ๊ณ์ ๋ฉ๋๋ค. <br/>
ํ๊ฒฐ๊ฐ์ ๋ง๊ณผ ์๋น์ค๋ก ์ฌ๋ฌ๋ถ ํ๋ถ ํ๋ถ ๋ชจ์๊ณ ์์ต๋๋ค.
</p>
</div>
<div id="second"></div>
<div id="third"></div>
</body>
</html>
02.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํจ์</title>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
//DOM : ์์ฑ, ์ญ์ , ์ฝ์
, ์์
$(function() {
$('#first > p').append("์ผ๋ฑํ์ง ์ผ๋ฑ์๋น์ค ๋ณต์ ");
$('#first > p').append("<h3> ์ผ๋ฑํ์ง ์ผ๋ฑ์๋น์ค ๋ณต์ </h3>");
// ์์ํ๊ทธ์ ๋งจ ์๋์ ๋ถ๋๋ค.
$('#first > ul').append("<li style='color:red'>์ก์ฑ๋ง๋</li>");
// ์์ํ๊ทธ์ ๋งจ ์์ ๋ถ๋๋ค.
$('#first > ul').prepend("<li style='color:blue'>์ฐ๋ง๋</li>");
//ulํ๊ทธ์ ๋์์ผ๋ก ๋ถ๋๋ค.
$('#first > ul').after("<h2>์๋</h2>");
//ulํ๊ทธ์ ํ์ผ๋ก ๋ถ๋๋ค.
$('#first > ul').before("<h3>๋ก๋ณถ์ด</h3>");
$("<h3> ์ผ๋ฑํ์ง ์ผ๋ฑ์๋น์ค ๋ณต์ </h3>").appendTo('#first > p');
$("<h3> ์ผ๋ฑํ์ง ์ผ๋ฑ์๋น์ค ๋ณต์ </h3>").prependTo('#first > p');
$('<h2> ์๋2 </h2>').insertAfter("#first > ul");
$('<h2> ๋ก๋ณถ์ด2 </h2>').insertBefore("#first > ul");
});
</script>
<body>
<div id="first">
์๋
ํ์ธ์. <br/>
<p>
๋ง์ ํ์ ์ ์ด๋ฃฉํ๊ธฐ ์ํ
์ฐ๋ฆฌ ๋ณต์ฑ์ ๋ง๋์ ๋
ธ๋ ฅ์ ๊ณ์ ๋ฉ๋๋ค. <br/>
ํ๊ฒฐ๊ฐ์ ๋ง๊ณผ ์๋น์ค๋ก ์ฌ๋ฌ๋ถ ํ๋ถ ํ๋ถ ๋ชจ์๊ณ ์์ต๋๋ค.
</p>
<b>๋ง๋ ๋ฉ๋ด</b>
<ul>
<li>๋ฌผ๋ง๋</li>
<li>๊ตฐ๋ง๋</li>
</ul>
</div>
</body>
</html>
03.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํจ์</title>
</head>
<style type="text/css">
img{
width:250px;
height:200px;
margin:5px;
}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function() {
$('#btn').click(function(){
//๋๋คํจ์ ์ฌ์ฉํด์ 1~3๊น์ง ๋์ค๊ฒ๋
var n = Math.ceil(Math.random()*3);
$('.img_may').append("<img src='../img/nature_"+ n +".jpg'/>");
});
});
</script>
<body>
<a href="#" id="btn">ํด๋ฆญ</a>
<div class="img_may"></div>
</body>
</html>
04.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํจ์</title>
</head>
<style type="text/css">
img{
width:250px;
height:200px;
margin:5px;
}
</style>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
//์ด๋ฏธ์ง ํด๋ฆญ์๋ง๋ค ๋งจ ๋ค์ ์ด๋ฏธ์ง๊ฐ ์์ผ๋ก ๋ถ๋๋ค.
$('img').click(function(){
$('.img_may').prepend($('img:last'));
});
});
</script>
<body>
<div class="img_may">
<img src="../img/nature_1.jpg"/>
<img src="../img/nature_2.jpg"/>
<img src="../img/nature_3.jpg"/>
</div>
</body>
</html>
05.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํจ์</title>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
$('#content a:first').click(function(){
$('h2').replaceWith("<p style='color:red;'>๋ด ์ฌ๋ฆ ์ถ์ ์๋ด</p>");
$('.theme_4 > li').remove();
$('<p style="color:blue;">์ธ์ฒ์ก๋</p>').replaceAll($('.theme b:eq(1)'));
});
});
</script>
<body>
<div>
<ul id="content">
<li><a href="#">์จ๋ฌ๋จผํธ ๊ต์ฒด</a></li>
<li><a href="#">์ค๋น์ค์ธ ์ถ์ </a></li>
</ul>
</div>
<h2>์ฃผ์์ง์ญ๋ณ ์ถ์ ์ ๋ณด์๋ด</h2>
<ul class="theme">
<li>
<b>์์ธ๋ฐ ๊ทผ๊ต์ง์ญ</b>
<ul class="theme_1">
<li>๊ฐํ ๊ณ ๋ ค์ฐ ์ง๋ฌ๋ ์ถ์ </li>
<li>๊ณ ์ ๊ตญ์ ๊ฝ ๋ฐ๋ํ</li>
<li>์๋ฒ๋๋ ํค๋ฆฝ์ถ์ </li>
<li>์ฐ์ฒ ์ ๊ณก๋ฆฌ ๊ตฌ์๊ธฐ ์ถ์ </li>
</ul>
</li>
<li>
<b>๊ฐ์๋</b>
<ul class="theme_2">
<li>๊น์ ์ ๋ฌธํ์ </li>
<li>์ถ์ฒ ๋ง์ ์ถ์ </li>
</ul>
</li>
<li>
<b>์ถฉ์ฒญ๋</b>
<ul class="theme_3">
<li>๋ณด๋ น๋จธ๋์ถ์ </li>
<li>ํ์ ํ์ธ๋ฆฝ ๊ฝ ์ถ์ </li>
</ul>
</li>
<li>
<b>์ ๋ผ๋</b>
<ul class="theme_4">
<li>์ง๋ ์ ๋น์ ๋ฐ๋ท๊ธธ ์ถ์ </li>
<li>์์ฒ๋ง ๊ตญ์ ์ ์ ๋ฐ๋ํ</li>
<li>ํจํ ๋๋น ์ถ์ </li>
</ul>
</li>
</ul>
</body>
</html>
06.DOM.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery - DOM ํ์ ๋ฉ์๋</title>
</head>
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function(){
var arr = new Array();
$('#first').children().each(function (i, e){
//arr.push(i + ":" + e.tagName + " " + $(e).text());
});
$('#first').children('span').each(function(i, e){
//arr.push(i + ":" + e.tagName + " " + $(e).text());
});
// 0:SPAN ์๋ฐ , 1:SPAN ๋ฉ๋ก
$('#first div').children('span').each(function(i, e){
//arr.push(i + ":" + e.tagName + " " + $(e).text());
});
// 0:B ์๋ฉ๋ฆฌ์นด๋
ธ ์ปคํผ
$('#first div').children('b').each(function(i, e){
//arr.push(i + ":" + e.tagName + " " + $(e).text());
});
// 0:SPAN ์๋ฐ , 1:SPAN ๋ฉ๋ก , 2:B ์๋ฉ๋ฆฌ์นด๋
ธ ์ปคํผ
$('#first').children('div').children().each(function(i, e){
arr.push(i + ":" + e.tagName + " " + $(e).text());
});
alert(arr.join("\n"));
});
</script>
<body>
<div>์ฌ๊ณผ</div>
<div id="first">
<span>๋ฐ๋๋</span>
<span>๋ธ๊ธฐ</span>
<div>
<span>์๋ฐ</span>
<span>๋ฉ๋ก </span>
</div>
<div>
<b>์๋ฉ๋ฆฌ์นด๋
ธ ์ปคํผ</b>
</div>
</div>
</body>
</html>
๋ฐ์ํ
'KITRI > JAVASCRIPT' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JavaScript ์ ์ดํด - [jQuery] Theme (0) | 2020.07.22 |
---|---|
JavaScript ์ ์ดํด - [jQuery] DOM ํจ์(2) (0) | 2020.07.22 |
JavaScript ์ ์ดํด - [jQuery] Selector(2) : ํํฐ (0) | 2020.07.21 |
JavaScript ์ ์ดํด - [jQuery] Selector(1) : ๊ธฐ๋ณธ, ์์(์์/ํ์ ) (0) | 2020.07.20 |
JavaScript ์ ์ดํด - [AJAX] Kakao ์ง๋ Javscript API ์ด์ฉํด์ ์ง๋๊ทธ๋ฆฌ๊ธฐ (0) | 2020.07.20 |
Comments