Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[10] 230110 [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 10์ผ] Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ: 8. ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๋ณ€ํ™˜ ๋ณธ๋ฌธ

๐ŸŒณBootcamp Revision 2023โœจ/JavaScript, jQuery, Ajax, JSON

[10] 230110 [K-๋””์ง€ํ„ธ ํŠธ๋ ˆ์ด๋‹ 10์ผ] Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ: 8. ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๋ณ€ํ™˜

yjyuwisely 2023. 1. 10. 12:50

230110 Tue 10th class

Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ
์ง„๋„: p. 294 ~  (๊ต์žฌ: HTML5 + CSS3 + JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ, ์ €์ž: ์ฒœ์ธ๊ตญ)

 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์ฒœ์ธ๊ตญ - ๊ต๋ณด๋ฌธ๊ณ 

HTML5+CSS3+JavaScript๋กœ ๋ฐฐ์šฐ๋Š” ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ๊ธฐ์ดˆ | ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ดˆ๋ณด์ž๋„ ์‰ฝ๊ฒŒ ๋ฐฐ์šฐ๋Š” ์›น ๊ฐœ๋ฐœ ์ง€์นจ์„œ!์ด ์ฑ…์€ ์›นํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ฒ˜์Œ ๋ฐฐ์šฐ๋Š” ์ดˆ๋ณด์ž๋“ค์„ ์œ„ํ•ด ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ์„ค๋ช…ํ•œ๋‹ค. 1~3์žฅ์—

product.kyobobook.co.kr

์˜ค๋Š˜ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๊ธฐ์–ตํ•  ๊ฒƒ์„ ์ •๋ฆฌํ–ˆ๋‹ค.


์ง€๋‚œ ์ˆ˜์—… ๋•Œ ๋ฐฐ์šด ๊ฒƒ ์ค‘ ๋‹ค์‹œ ๊ธฐ์–ตํ•  ๊ฒƒ


- CSS ์ ์šฉ
ex)

<style type = "text/css">div{text-align:center; background-color:#E5FFCC}
</style>

 

- ํฐํŠธ import ํ•˜๊ธฐ

2023.01.03 - [1. Revision 2023/CSS3] - [5] 230103 Ch4 CSS3 ์Šคํƒ€์ผ ์‹œํŠธ ๊ธฐ์ดˆ

<style type="text/css">
@import url('http://fonts.googleapis.com/css2?family=Rubik+Bubbles&display=swap');

h1{background-color:#FFE5CC; 
color: red;
text-shadow: 5px 5px 12px orange;
font: italic 30px 'Rubik Bubbles', cursive;}
</style>

 

display:inline;/* block ์š”์†Œ -> inline ์š”์†Œ๋กœ ๋ณ€๊ฒฝ, ์ˆ˜ํ‰์œผ๋กœ ๋œ๋‹ค. */

- ++x: x์˜ ๊ฐ’ x=x+1
ex) 3->4
 

--x: x์˜ ๊ฐ’ x=x-1
ex) 4->3


-alert(๋ฌธ์žฅ): ์‚ฌ์šฉํ•˜๋ฉด alert์ฐฝ์ด ๋œจ๋ฉด์„œ ๊ทธ ์•ˆ์— ๋ฌธ์žฅ์ด ๋œฌ๋‹ค.


- Get ๋ฐฉ์‹๊ณผ Post ๋ฐฉ์‹ p. 82 

2023.01.02 - [1. Revision 2023/HTML5] - [4] 230102 Ch3 HTML5 ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด & ์ž…๋ ฅ์–‘์‹

ํด๋ผ์ด์–ธํŠธ ์ปดํ“จํ„ฐ๊ฐ€ ์„œ๋ฒ„ ์ปดํ“จํ„ฐ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ์‹์—๋Š” HTTP GET์š”์ฒญ๊ณผ HTTP POST์š”์ฒญ์ด ์žˆ๋‹ค.

  • Get ๋ฐฉ์‹
    URL ์ฃผ์†Œ ๋’ค์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ถ™์—ฌ์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค. (URL(Uniform Resource Locator): ์ธํ„ฐ๋„ท์—์„œ ์›น ํŽ˜์ด์ง€, ์ด๋ฏธ์ง€, ๋น„๋””์˜ค ๋“ฑ ๋ฆฌ์†Œ์Šค์˜ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ฌธ์ž์—ด)
    Get ๋ฐฉ์‹์ด Post ๋ฐฉ์‹๋ณด๋‹ค ๋น„๊ต์  ์ „์†ก๋˜๋Š” ์†๋„๊ฐ€ ๋น ๋ฅด๋‚˜ ๋ณด์•ˆ์—๋Š” ์ทจ์•ฝํ•˜๋‹ค.
    method๋ฅผ ๋”ฐ๋กœ ์–ธ๊ธ‰ ์•ˆ ํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ Get ๋ฐฉ์‹์ด๋‹ค. 

    ex) ๊ฒ€์ƒ‰์–ด
    ex) file:///D:/221229/html/WebContent/ch03_02_req.html?id=1234&password=5678

  • Post ๋ฐฉ์‹
    ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ URL ์ฃผ์†Œ์— ๋ถ™์ด์ง€ ์•Š๊ณ  HTTP Request ํ—ค๋”์— ํฌํ•จ์‹œ์ผœ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹์ด๋‹ค.
    Post ๋ฐฉ์‹์ด Get ๋ฐฉ์‹๋ณด๋‹ค ๋ณด์•ˆ์—๋Š” ๊ฐ•ํ•˜๋‚˜, ์†๋„๋Š” ๋Š๋ฆฌ๋‹ค.
    ๊ธธ์ด ์ œํ•œ์ด ์—†์œผ๋ฉฐ, ๋ณด์•ˆ์ด ์œ ์ง€๋œ๋‹ค.  
    ex) ๋กœ๊ทธ์ธ ex) file:///D:/221229/html/WebContent/ch03_02_req.html

Ch. 8 ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ์ดˆ

์ฑ…์˜ ๋ชฉ์ฐจ
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์†Œ๊ฐœ
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์šฉ๋„
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์œ„์น˜
-๋ฌธ์žฅ
-๋ณ€์ˆ˜
-์ž๋ฃŒํ˜•
-์—ฐ์‚ฐ์ž
-์ˆซ์ž์™€ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๋ณ€ํ™˜
-์กฐ๊ฑด๋ฌธ
-๋ฐ˜๋ณต๋ฌธ
-๋ฐฐ์—ด
-ํ•จ์ˆ˜
-์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์ž…์ถœ๋ ฅ

1. ์ˆซ์ž์™€ ๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๋ณ€ํ™˜ p. 294

1.1 prompt() ํ•จ์ˆ˜

์‚ฌ์šฉ์ž์—๊ฒŒ ์–ด๋–ค ์‚ฌํ•ญ์„ ์•Œ๋ ค์ฃผ๊ณ  ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ต๋ณ€์„ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ์œˆ๋„์šฐ๋ฅผ ํ™”๋ฉด์— ๋„์šด๋‹ค.

prompt("๋นˆ์นธ ์œ„์— ๋œจ๋Š” ๋ฌธ์žฅ", "๋นˆ์นธ์— ๋œจ๋Š” ๋ฌธ์žฅ")



์˜ˆ์‹œ) 

<script>
var age = prompt("๋‚˜์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”.", "๋งŒ๋‚˜์ด๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.");
</script>

=: ์˜ค๋ฅธ์ชฝ์˜ ๊ฐ’์„ ์™ผ์ชฝ์— ์ €์žฅํ•˜๋Š” ๋Œ€์ž… ์—ฐ์‚ฐ์ž์ด๋‹ค.

์ด์ „ ์ˆ˜์—…์—์„œ ๋ฐฐ์šด
2023.01.02 - [1. Revision 2023/HTML5] - [4] 230102 Ch3 HTML5 ๋ฉ€ํ‹ฐ๋ฏธ๋””์–ด & ์ž…๋ ฅ์–‘์‹

ID : <input type="text" required placeholder="์•„์ด๋””๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”">


์™€ ๋น„์Šทํ•˜๋‹ค. 

๊ฒฐ๊ณผ) 



1.2 parseInt()

๋ฌธ์ž์—ด์„ ์ˆซ์ž๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

parse:  to divide (a sentence) into grammatical parts and identify the parts and their relations to each other
[Computing] analyse (a string or text) into logical syntactic components
syntactic: ๊ตฌ๋ฌธ๋ก ์˜(์–ธ์–ด ๋‹จ์–ด ๊ฒฐํ•ฉ์œผ๋กœ ์ด๋ฃจ์–ด์ง€๋Š” ๋ฌธ์žฅ(ๆ–‡็ซ )์˜ ๊ตฌ์กฐ๋‚˜ ๊ตฌ๋ฌธ(ๆง‹ๆ–‡) ์š”์†Œ๋ฅผ ๋ถ„์„ํ•จ์œผ๋กœ์จ, ๊ฐ ๋ฌธ์žฅ ์„ฑ๋ถ„์ด ๊ตฌ์„ฑ๋˜๋Š” ๊ทœ์น™์„ ์—ฐ๊ตฌํ•˜๋Š” ๋ถ„์•ผ.)

 

1.2 String()

์ˆซ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค.

 

1.3 function add(){ ... } 

์‚ฌ์šฉ์ž ์ •์˜ ํ•จ์ˆ˜: ๋‚ด๊ฐ€ ๋งŒ๋“  ํ•จ์ˆ˜
user-defined function (UDF) is function provided by the user of a program or environment, in a context where the usual assumption is that functions are built into the program or environment.

๋ณ€์ˆ˜ ํ•จ์ˆ˜
- ๋ณ€์ˆ˜ ์„ ์–ธ: var x=10;
๋ณ€์ˆ˜ ์ด๋ฆ„(ex. x)์€ ์˜๋ฏธ ์žˆ๊ฒŒ, ๋งˆ์Œ๋Œ€๋กœ ์ง€์–ด๋„ ๋œ๋‹ค.



- ๋ณ€์ˆ˜ ํ˜ธ์ถœ(์‚ฌ์šฉ): x
- ํ•จ์ˆ˜ ์„ ์–ธ: function ํ•จ์ˆ˜๋ช…
ํ•จ์ˆ˜ ์ด๋ฆ„(ex. add(), calc())๋„ ์˜๋ฏธ ์žˆ๊ฒŒ, ๋งˆ์Œ๋Œ€๋กœ ์ง€์–ด๋„ ๋œ๋‹ค.
function add(){
...
}
- ํ•จ์ˆ˜ ํ˜ธ์ถœ: add();

1.4 HTML ์š”์†Œ์˜ ์„ ํƒ

HTML ์š”์†Œ๋ฅผ ์„ ํƒํ•˜๊ธฐ ์œ„ํ•ด ์ œ๊ณต๋˜๋Š” ๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

๋ฉ”์„œ๋“œ ์„ค๋ช…
document.getElementsByTagName(ํƒœ๊ทธ์ด๋ฆ„) ํ•ด๋‹น ํƒœ๊ทธ ์ด๋ฆ„์˜ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•œ๋‹ค.
document.getElementById(์•„์ด๋””) ํ•ด๋‹น ์•„์ด๋””์˜ ์š”์†Œ๋ฅผ ์„ ํƒํ•œ๋‹ค.
document.getElementsByClassName(ํด๋ž˜์Šค์ด๋ฆ„) ํ•ด๋‹น ํด๋ž˜์Šค์— ์†ํ•œ ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•œ๋‹ค.
document.getElementsByName(name์†์„ฑ๊ฐ’) ํ•ด๋‹น name ์†์„ฑ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•œ๋‹ค.
document.querySelectorAll(์„ ํƒ์ž) ํ•ด๋‹น ์„ ํƒ์ž๋กœ ์„ ํƒ๋˜๋Š” ์š”์†Œ๋ฅผ ๋ชจ๋‘ ์„ ํƒํ•œ๋‹ค.

 

getElementById()

The getElementById() method returns an element with a specified value and returns null if the element does not exist.

 

document.getElementById().value

์•„์ด๋”” ์„ ํƒ์ž(#)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
value
: value, ์ฆ‰ ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
.value๋ฅผ ์•ˆ ํ•˜๋ฉด input ํƒœ๊ทธ ์ „์ฒด๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
 
The document object represents your web page.
If you want to access any element in an HTML page, you always start with accessing the document object.

 

document.getElementByClassName()

ํด๋ž˜์Šค ์„ ํƒ์ž(.)๋ฅผ ์˜๋ฏธํ•œ๋‹ค.

 

document.getElementByTagName()

The getElementsByTagName method of Document interface returns an HTMLCollection of elements with the given tag name.

querySelector()

The querySelector() method returns the first child element that matches a specified CSS selector(s) of an element. 
Note: The querySelector() method only returns the first element that matches the specified selectors. To return all the matches, use the querySelectorAll() method instead.
CSS์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

querySelectorAll()

querySelector์™€ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์€ ๋™์ผํ•˜๋ฉฐ ์„ ํƒ์ž๋ฅผ ์„ ํƒํ•˜์—ฌ ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ๊ฐ์ฒด์ธ nodeList๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
The querySelectorAll() method returns all elements that matches a CSS selector(s) and returns a NodeList.

์ฐธ๊ณ : https://www.w3schools.com/jsref/met_element_queryselector.asp


1.4.1 add ๊ณ„์‚ฐ๊ธฐ
์˜ˆ์‹œ)

<script>
//ํ•จ์ˆ˜ ์„ ์–ธ 
function calc(){
var x = document.getElementById("x").value; //์ฒซ๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’
var y = document.getElementById("y").value; //๋‘๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’ 
var sum;
sum= parseInt(x) + parseInt(y);
document.getElementById("sum").value = sum;} //sum๊ฐ’์„ ํ•ฉ๊ณ„์— ๋Œ€์ž…ํ•˜๊ธฐ 
</script>
</head>

<body>
<h3>๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ</h3>
<form action="..." name="myform" method="get">
์ฒซ๋ฒˆ์งธ ์ •์ˆ˜: <input type="text" id="x"><br>
๋‘๋ฒˆ์งธ ์ •์ˆ˜: <input type="text" id="y"><br>
ํ•ฉ๊ณ„: <input type="text" id="sum"><br>
<input type="button" value="๊ณ„์‚ฐ" onclick="calc()">
</form>
</body>

var x = document.getElementById("x").value;: id๊ฐ€ "x"์ธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
document.getElementById("sum").value = sum;: id๊ฐ€ "sum"์ธ ์š”์†Œ์— ๋ณ€์ˆ˜ sum์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค.


onclick="calc()":
๋ฒ„ํŠผ์ด ํด๋ฆญ๋˜๋ฉด calc()๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ(ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์„œ ์‹คํ–‰ํ•œ๋‹ค.)ํ•œ๋‹ค.


๊ฒฐ๊ณผ) 

- Number()์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. The Number() constructor creates a Number object. 
var sum = Number(x) + Number(y);

- querySelector()๋ฅผ ์‚ฌ์šฉํ•ด์„œ
CSS ๋ฐฉ์‹์œผ๋กœ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. 

<script>
//ํ•จ์ˆ˜ ์„ ์–ธ 
function calc(){
var x = document.querySelector('#x').value;
var y = document.querySelector('#y').value;
var sum;
sum= parseInt(x) + parseInt(y);
document.querySelector("#sum").value = sum;
}
</script>
</head>

<body>
<h3>๋ง์…ˆ ๊ณ„์‚ฐ๊ธฐ</h3>
<form action="..." name="myform" method="get">
์ฒซ๋ฒˆ์งธ ์ •์ˆ˜: <input type="text" id="x"><br>
๋‘๋ฒˆ์งธ ์ •์ˆ˜: <input type="text" id="y"><br>
ํ•ฉ๊ณ„: <input type="text" id="sum"><br>
<input type="button" value="๊ณ„์‚ฐ" onclick="calc()">
</form>
</body>

1.4.2 ์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ ๋งŒ๋“ฆ๊ธฐ
์˜ˆ์‹œ)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Assignment calculator p.296</title>
<style type = "text/css">
div{text-align:center; background-color:#E5FFCC;}

</style>
<script> 
//ํ•จ์ˆ˜ ์„ ์–ธ 
function calc_add(){
var x = document.getElementById("x").value; //์ฒซ๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’
var y = document.getElementById("y").value; //๋‘๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’ 
var sum;
sum= parseInt(x) + parseInt(y);
document.getElementById("sum").value = sum;} //sum๊ฐ’์„ ํ•ฉ๊ณ„์— ๋Œ€์ž…ํ•˜๊ธฐ 

function calc_sub(){
var x = document.getElementById("x").value; //์ฒซ๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’
var y = document.getElementById("y").value; //๋‘๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’  
var sum;
sum= parseInt(x) - parseInt(y);
document.getElementById("sum").value = sum;} //sum๊ฐ’์„ ํ•ฉ๊ณ„์— ๋Œ€์ž…ํ•˜๊ธฐ 

function calc_div(){
var x = document.getElementById("x").value; //์ฒซ๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’
var y = document.getElementById("y").value; //๋‘๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’ 
var sum;
sum= parseInt(x) * parseInt(y);
document.getElementById("sum").value = sum;} //sum๊ฐ’์„ ํ•ฉ๊ณ„์— ๋Œ€์ž…ํ•˜๊ธฐ 

function calc_mul(){
var x = document.getElementById("x").value; //์ฒซ๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’
var y = document.getElementById("y").value; //๋‘๋ฒˆ์งธ ์ •์ˆ˜๊ฐ’ 
var sum;
sum= parseInt(x) / parseInt(y);
document.getElementById("sum").value = sum;} //sum๊ฐ’์„ ํ•ฉ๊ณ„์— ๋Œ€์ž…ํ•˜๊ธฐ 
</script>
</head>

<body>
<div>
<h3>์‚ฌ์น™์—ฐ์‚ฐ ๊ณ„์‚ฐ๊ธฐ</h3>
์ •์ˆ˜๋งŒ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.<br><br>
<form action="..." name="myform" method="get">
์ฒซ๋ฒˆ์งธ ์ˆซ์ž: <input type="text" id="x"><br>
๋‘๋ฒˆ์งธ ์ˆซ์ž: <input type="text" id="y"><br>
<input type="button" value=" + " onclick="calc_add()">
<input type="button" value=" - " onclick="calc_sub()">
<input type="button" value=" / " onclick="calc_mul()">
<input type="button" value=" * " onclick="calc_div()"><br>
ํ•ฉ๊ณ„: <input type="text" id="sum"><br>
</form>
</div>
</body>
</html>

๊ฒฐ๊ณผ)


1.4.3 ์ƒํ’ˆ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ ๊ฐ์†Œ ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ

์˜ˆ์‹œ)
๋‚ด๊ฐ€ ๋งŒ๋“  ์ฝ”๋“œ)
์ดˆ๊ธฐ์— ์‹œํ–‰์ฐฉ์˜ค๋ฅผ ๊ฒช์—ˆ๋Š”๋ฐ ๊ทธ ์ด์œ ๋Š” document.getElementById("first").value = number;์—์„œ "first"๋ฅผ "x"๋กœ ์ž˜๋ชป ๋„ฃ์–ด์„œ๋‹ค. ๋ณ€์ˆ˜ ์ด๋ฆ„์„ ์ฒ˜์Œ์— x, y, sum์œผ๋กœ ์ผ๋”๋‹ˆ ๋” ํ—ท๊ฐˆ๋ฆฐ ๊ฒƒ ๊ฐ™๋‹ค. ์‹ ์ค‘ํ•˜๊ฒŒ ๊ณจ๋ผ์•ผ๊ฒ ๋‹ค.

<script>์— ์žˆ๋Š” ์ œ์ผ ๋งˆ์ง€๋ง‰ ์ฝ”๋“œ document.getElementById("first").value = number;์— ์žˆ๋Š” "first"๊ฐ€ ์ œ์ผ ๋งˆ์ง€๋ง‰์— ์–ป๋Š” ์ตœ์ข… value์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ <body>์— ์žˆ๋Š” ๊ฐœ์ˆ˜: <input type="text" id="first" value="0"> ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด id="first"๊ฐ€ ์‚ฌ์šฉ๋๋‹ค.

๊ฐœ์ˆ˜:<input type="number" min="0" step="1" value="0">๋Š” ์ง€๋‚œ ์ˆ˜์—…์—์„œ ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ(Ch7)์—์„œ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์ด๋‹ค. ์„ธ๋กœํ˜• ํ™”์‚ดํ‘œ๋กœ ์ˆ˜๋Ÿ‰ ์กฐ์ ˆ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋‚˜์˜จ๋‹ค. 

<style type ="text/css">
div{text-align:center; background-color:#E5FFCC;}
</style>
<script>
function calc_add(){
//id๊ฐ€ "first"์ธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. value -> "first"
var x = document.getElementById("first").value; 
var number = parseInt(first) + 1;
//id๊ฐ€ "first"์ธ ์š”์†Œ์— ๋ณ€์ˆ˜ number์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค. number -> "first"
document.getElementById("first").value = number;
} 

function calc_sub(){
//id๊ฐ€ "first"์ธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
var x = document.getElementById("first").value; 
var number = parseInt(first) - 1;
//id๊ฐ€ "first"์ธ ์š”์†Œ์— ๋ณ€์ˆ˜ number์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค.
document.getElementById("first").value = number;
} 
</script>
</head>

<body>
<div>
<h3>์ƒํ’ˆ์˜ ๊ฐœ์ˆ˜</h3>
๊ฐœ์ˆ˜:<input type="number" min="0" step="1" value="0"><br>
<br>

<form name="myform" action="..." method="get">
//์ดˆ๊ธฐ๊ฐ’ = 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. 
๊ฐœ์ˆ˜: <input type="text" id="first" value="0">

<input type="button" value=" + " onclick="calc_add()">
<input type="button" value=" - " onclick="calc_sub()"><br>
</form>
</div>
</body>


์ˆ˜์—…์˜ ์ฝ”๋“œ) ์ง€๋‚œ ์ˆ˜์—…์— ๋ฐฐ์šด ++x, --x๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ฝ”๋“œ๊ฐ€ ๋” ์งง๊ณ  ๊ฐ„๊ฒฐํ•˜๋‹ค. 

<script>
function calc_add(){
//id๊ฐ€ "first"์ธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
var x = document.getElementById("first").value; 
//id๊ฐ€ "first"์ธ ์š”์†Œ์— ๋ณ€์ˆ˜ ++x์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค.
document.getElementById("first").value=++x;
} 

function calc_sub(){
//id๊ฐ€ "first"์ธ ์š”์†Œ์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค.
var x = document.getElementById("first").value; 
//id๊ฐ€ "first"์ธ ์š”์†Œ์— ๋ณ€์ˆ˜ --x์˜ ๊ฐ’์„ ๋Œ€์ž…ํ•œ๋‹ค.
document.getElementById("first").value=--x;
} 
</script>
</head>

<body>
<div>
<h3>์ƒํ’ˆ์˜ ๊ฐœ์ˆ˜</h3>
๊ฐœ์ˆ˜:<input type="number" min="0" step="1" value="0"><br>
<br>

<form name="myform" action="..." method="get">
//์ดˆ๊ธฐ๊ฐ’ = 0์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. 
๊ฐœ์ˆ˜: <input type="text" id="first" value="0">

<input type="button" value=" + " onclick="calc_add()">
<input type="button" value=" - " onclick="calc_sub()"><br>
</form>
</div>
</body>

๊ฒฐ๊ณผ)


728x90
๋ฐ˜์‘ํ˜•
Comments