๐ป/Javascript
(50) [JavaScript] 6์ผ JavaScript ๊ฐ์ฒด - DOM
๋ฐ๊ถ
2024. 4. 28. 12:34
๋ฌธ์ ๊ฐ์ฒด ๋ชจ๋ธ(Document Object Model)์ดํด
์น ํ์ด์ง ์ฝ์ฒธ์ธ ๋ฅผ ์กฐ์ํ๋ ๋ฉ์๋์ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
DOM์ HTML๋ฌธ์์ ๋ชจ๋ ์์์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ์ ์ํ API๋ค.
DOM์ ์ ์ฒด ํ์ด์ง๋ฅผ ๋ ธ๋ ๊ณ์ธต ๊ตฌ์กฐ๋ก ๋ณํํ๋ฉฐ, HTML ํ์ด์ง์ ๊ฐ ๋ถ๋ถ์ ๊ฐ๊ธฐ ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ
ํฌํจํ๋ ๋ค์ํ ํ์ ์ ๋ ธ๋๋ก ํํ๋๋ค.
</head>
<body>
<h3>document ๊ฐ์ฒด</h3>
<form name="myForm">
<p>
์ด๋ฆ : <input type="text" name="name" >
</p>
<p>
์์ผ : <input type="text" name="birth">
</p>
<p>
๋์ด : <input type="text" name="age" id="age">
</p>
<p>
<button type="button" onclick="result()">ํ์ธ</button>
</p>
</form>
<hr>
<div class="Layout"></div>
<script type="text/javascript">
function result() {
//ํผ ์ ๊ทผ 1. ๋ฐฐ์ด ์ ๊ทผ 2. ํผ ์ด๋ฆ์ผ๋ก ์ ๊ทผ
//const f= document.forms //'s'๋ ๋ฐฐ์ด ๊ฐ๋
, ๋ชจ๋ ํผ์ ๊ฐ์ ธ์ด
const f = document.myForm; // ํผ ์ด๋ฆ์ผ๋ก ์ ๊ทผ (์ด๋ฆ์ ๋๊ฐ์ ๊ฒ์ด ์์ผ๋ฉด ์๋จ)
//form์ ์ด์ฉํ์ฌ form ์๋์ ์
๋ ฅ
let name = f.name.value;
//getElementsByName() ํจ์๋ฅผ ์ด์ฉํ์ฌ name ์์ฑ์ผ๋ก ์ ๊ทผ
let birth = document.getElementsByName('birth')[0].value;
//getElementsByID() ํจ์๋ฅผ ์ด์ฉํ์ฌ id ์์ฑ์ผ๋ก ์ ๊ทผ
let age = document.getElementById('age').value;
let s = `์ด๋ฆ:${name},์๋
์์ผ:${birth},๋์ด:${age}`;
//์ ํ์๋ก ์ ๊ทผ
//let divEL = document.querySelectorAll('.layout')[0];
let divEL = document.querySelector('.layout');
divEL.innerHTML=s;
}
</script>
</body>
</html>
<style type="text/css">
.container{
background:#fff; width: 100% ; display : flex;
justify-content:center; align-items:center;
}
</style>
</head>
<body>
<h3>์ ์ฒด ํ๋ฉด</h3>
<div class ="container">
<button type="button" class="btnFullscreen">์ ์ฒด ํ๋ฉด</button>
<button type="button" class="btnExitFullscreen">์ ์ฒดํ๋ฉด์ทจ์</button>
</div>
<script type="text/javascript">
const btnEnterFull=document.querySelector('.btnFullscreen');
const btnExitFull=document.querySelector('.btnExitFullscreen');
const container = document.querySelector('.container');
//๋ฒํผ์ ๋ํ click ์ด๋ฒคํธ ๋ฑ๋ก
btnEnterFull.addEventListener('click',e=>{
container.requestFullscreen();
});
btnEnterFull.addEventListener('click',e=>{
document.exitFullscreen();
});
</script>
</body>
</html>
</head>
<body>
<h3>location ๊ฐ์ฒด</h3>
<!--
ํ์ฌ ๋ธ๋ผ์ฐ์ ์ ํ์๋ HTML ๋ฌธ์์ ์ฃผ์๋ฅผ ์ด๊ฑฐ๋
๋ธ๋ผ์ฐ์ ์ ์๋ฌธ์๋ฅผ ๋ถ๋ฌ์ฌ๋ ์ฌ์ฉ
-->
<p> <button type ="button" onclick="fun();">๋ค์ด๋ฒ</button></p>
<p> <button type ="button" onclick="fun2();">๊ตฌ๊ธ</button></p>
<p> <button type ="button" onclick="location.href('https://www.daum.net/')">๋ค์</button> </p>
<script type="text/javascript">
function fun() {
//์ด์ ํ์ด์ง๋ก ๋์๊ฐ์ ์๋ค.
location.href='https://naver.com';
}
function fun2() {
//์ด์ ํ์ด์ง๋ก ๋์๊ฐ์์๋ค. ์๋ก์ด ํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๊ธฐ์ ์ ํ์คํ ๋ฆฌ ์ ๊ฑฐ
location.replace('https://google.com');
}
</script>
</body>
</html>
</head>
<body>
<h3>navigator</h3>
<!-- ๋ธ๋ผ์ฐ์ ๊ณต๊ธ์ ๋ฐ ๋ฒ์ ์ ๋ณด๋ฑ์ ํฌํจํ ๋ธ๋ผ์ฐ์ ์ ๋ํ ๋ค์ํ ์ ๋ณด๋ฅผ ์ ์ฅํ๋ ๊ฐ์ฒด -->
<script type="text/javascript">
let a = navigator.userAgent;
let b = navigator.language;
console.log('userAgent:',a);
console.log('language:',b);
</script>
</body>
</html>
</head>
<body>
<div>
<p><a href="javascript:winOpen();">์ด๊ธฐ</a></p>
<p><a href="javascript:winClose();">๋ซ๊ธฐ</a></p>
</div>
<script type="text/javascript">
var win = null;
function winOpen() {
let flag;
let url='https://www.naver.com';
flag='left=10, top=10, width=700,height=500';
win = window.open(url,'test',flag);
}
function winClose() {
if(win){
win.close();
}
win=null;
}
</script>
</body>
</html>
</head>
<body>
<div>
<form name="myForm">
<p><input type="text" name="name" placeholder="์ด๋ฆ์ ์
๋ ฅํ์ธ์"></p>
<p>
<input type="text" name="zip" readonly placeholder="์ฐํธ๋ฒํธ">
<button type ="button" onclick="zipOpen();">์ฐํธ๋ฒํธ</button>
<input type="text" name="addr1" style="width:250px;" readonly placeholder="๊ธฐ๋ณธ์ฃผ์"><br>
<input type="text" name="addr2" style="width:250px;" placeholder="์์ธ์ฃผ์">
</p>
<p>
<button type="button">ํ์ธ</button>
</p>
</form>
</div>
<script type="text/javascript">
function zipOpen() {
let flag;
let url = 'zipFind.html';
flag='left=30, top=30,width=700, height=500';
window.open(url,'zip',flag);
}
</script>
</body>
</html>
</head>
<body>
<div class="Layout"
style="width: 300px; height: 300px; border: 3px dotted blue; margin: 30px auto;"></div>
<p style="text-align: center;">
<button type="button" onclick="result()">๋ฐฐ๊ฒฝ์ ๋ณ๊ฒฝ</button>
</p>
<script type="text/javascript">
function result() {
let url = 'colorWin.html';
let flag = 'top=30,left=30, width=250, height=100';
window.open(url, 'test', flag);
}
</script>
</body>
</html>