Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Y
YAN CHENGLONG
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
YAN CHENGLONG
YAN CHENGLONG
Commits
274cb6dd
Commit
274cb6dd
authored
4 years ago
by
YAN CHENGLONG
Browse files
Options
Downloads
Patches
Plain Diff
Add new file
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
HW210311_YAN CHENGLONG.html
+222
-0
222 additions, 0 deletions
HW210311_YAN CHENGLONG.html
with
222 additions
and
0 deletions
HW210311_YAN CHENGLONG.html
0 → 100644
+
222
−
0
View file @
274cb6dd
<!DOCTYPE html>
<html>
<head>
<title>
자바스크립트 계산기
</title>
<meta
charset=
"UTF-8"
>
<style>
p
,
input
{
font-family
:
monospace
;
}
p
{
white-space
:
pre
;
}
body
{
height
:
100%
;
display
:
flex
;
align-items
:
center
;
justify-content
:
center
;
background-color
:
#0e92b3
;
color
:
white
;
}
#myCalc
{
margin-top
:
5%
;
border
:
white
solid
3px
;
width
:
500px
;
border-radius
:
10px
;
}
.header
{
text-align
:
center
;
font-size
:
35px
;
text-transform
:
uppercase
;
line-height
:
100px
;
}
input
{
width
:
60%
;
height
:
50px
;
margin-bottom
:
20px
;
border-radius
:
5px
;
display
:
flex
;
border
:
white
solid
2px
;
margin-left
:
18%
;
text-align
:
center
;
font-size
:
20px
;
font-weight
:
bolder
;
}
#result
{
text-align
:
center
;
font-size
:
30px
;
font-weight
:
bolder
;
}
hr
{
background-color
:
white
;
}
</style>
</head>
<body>
<div
id=
"myCalc"
>
<div
class=
"header"
style=
"text-align: center;"
>
Calculate
</div>
<p><input
class=
"calc-x-input"
value=
"5"
></p>
<p><input
class=
"calc-symble-input"
value=
"+"
></p>
<!--添加符号-->
<p><input
class=
"calc-y-input"
value=
"0"
></p>
<p></p>
<div
id=
"result"
>
<hr>
<p>
=
<span
class=
"calc-result"
></span></p>
</div>
</div>
<script
type=
"text/javascript"
>
(
function
(){
function
Calc
(
xInput
,
yInput
,
symbleInput
,
output
)
{
this
.
xInput
=
xInput
;
this
.
yInput
=
yInput
;
//在对象中添加symbleInput属性
this
.
symbleInput
=
symbleInput
;
this
.
output
=
output
;
}
Calc
.
xName
=
'
xInput
'
;
Calc
.
yName
=
'
yInput
'
;
Calc
.
symbleName
=
'
symbleInput
'
;
Calc
.
prototype
=
{
render
:
function
(
result
)
{
this
.
output
.
innerText
=
String
(
result
);
}
};
function
CalcValue
(
calc
,
x
,
y
,
symble
)
{
this
.
calc
=
calc
;
this
.
x
=
x
;
this
.
y
=
y
;
//根据符号判断运算
//this.result = x + y;
this
.
symble
=
symble
;
console
.
log
(
"
cal函数内:
"
+
this
.
symble
);
if
(
this
.
symble
==
'
-
'
){
this
.
result
=
x
-
y
;
}
else
if
(
this
.
symble
==
'
+
'
){
this
.
result
=
x
+
y
;
}
else
if
(
this
.
symble
==
'
*
'
){
this
.
result
=
x
*
y
;
}
else
if
(
this
.
symble
==
'
/
'
){
this
.
result
=
x
/
y
;
}
else
{
this
.
result
=
"
symble wrong
"
;
}
}
CalcValue
.
prototype
=
{
//判断
copyWith
:
function
(
name
,
value
)
{
var
number
=
parseFloat
(
value
);
if
(
isNaN
(
number
)
||
!
isFinite
(
number
))
return
this
;
if
(
name
===
Calc
.
xName
)
return
new
CalcValue
(
this
.
calc
,
number
,
this
.
y
,
this
.
symble
);
if
(
name
===
Calc
.
yName
)
return
new
CalcValue
(
this
.
calc
,
this
.
x
,
number
,
this
.
symble
);
return
this
;
},
render
:
function
()
{
this
.
calc
.
render
(
this
.
result
);
},
//添加函数,判断新输入的符号
symble_copyWith
:
function
(
name
,
value
)
{
if
(
name
===
Calc
.
symbleName
)
return
new
CalcValue
(
this
.
calc
,
this
.
x
,
this
.
y
,
value
)
return
this
;
}
};
function
initCalc
(
elem
)
{
var
calc
=
new
Calc
(
elem
.
querySelector
(
'
input.calc-x-input
'
),
elem
.
querySelector
(
'
input.calc-y-input
'
),
//添加参数
elem
.
querySelector
(
'
input.calc-symble-input
'
),
elem
.
querySelector
(
'
span.calc-result
'
)
);
var
lastValues
=
new
CalcValue
(
calc
,
parseFloat
(
calc
.
xInput
.
value
),
parseFloat
(
calc
.
yInput
.
value
),
//添加参数
calc
.
symbleInput
.
value
);
var
handleCalcEvent
=
function
handleCalcEvent
(
e
)
{
var
newValues
=
lastValues
,
elem
=
e
.
target
;
switch
(
elem
)
{
case
calc
.
xInput
:
newValues
=
lastValues
.
copyWith
(
Calc
.
xName
,
elem
.
value
,
);
break
;
case
calc
.
yInput
:
newValues
=
lastValues
.
copyWith
(
Calc
.
yName
,
elem
.
value
);
break
;
case
calc
.
symbleInput
:
newValues
=
lastValues
.
symble_copyWith
(
Calc
.
symbleName
,
elem
.
value
);
break
;
}
if
(
newValues
!==
lastValues
){
lastValues
=
newValues
;
lastValues
.
render
();
}
};
elem
.
addEventListener
(
'
keyup
'
,
handleCalcEvent
,
false
);
return
lastValues
;
}
window
.
addEventListener
(
'
load
'
,
function
()
{
var
cv
=
initCalc
(
document
.
getElementById
(
'
myCalc
'
));
cv
.
render
();
},
false
);
}());
</script>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment