js 에서 eval () 방법 에 대해 계산기 의 작은 예 를 썼 다.
===================================================
인용 하 다.
정의 와 용법
eval () 함수 가 자 바스 크 립 트 문자열 을 계산 하고 스 크 립 트 코드 로 실행 합 니 다.
매개 변수 가 표현 식 이 라면 eval () 함수 가 표현 식 을 실행 합 니 다.인자 가 자바 script 구문 이 라면 eval () 은 자바 script 문 구 를 실행 합 니 다.
실례
JavaScript 코드 나 표현 식 실행:
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">eval("x=10;y=20;document.write(x*y)");
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">document.write("<br>" + eval("2+2"));
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">document.write("<br>" + eval(x+17));
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">
<br style="margin-bottom:0px;margin-left:0px;margin-right:0px;margin-top:0px;padding-bottom:0px;padding-left:0px;padding-right:0px;padding-top:0px;">
==============================================================================
계산 기 를 썼 습 니 다. eval () 로 표 시 된 표현 식 계산 결 과 를 가 져 왔 습 니 다.
<style type="text/css">
#box{
width: 406px;
height: 500px;
border: 1px solid black;
margin: 50px auto;
}
#screen{
width: 100%;
height: 100px;
font: 20px ;
text-align: right ;
/*overflow: hidden;*/
}
#top,#bottom {
width: 100%;
height: 49px;
border-bottom: 1px solid black;
float: left;
}
#number{
width: 306px;
float: left;
}
#mark{
width: 100px;
/*height: 00px;*/
float: left;
}
#number div{
width: 100px;
height: 100px;
border: 1px solid black;
float: left;
}
#mark div{
height: 100px;
border: 1px solid black;
}
.center {
text-align: center;
font: 20px/100px 'simhei';
}
</style>
<div id="box">
<div id="screen">
<div id="top"/>
<div id="bottom"/>
</div>
<div id="number" class="center">
<div value="1">1</div>
<div value="2">2</div>
<div value="3">3</div>
<div value="4">4</div>
<div value="5">5</div>
<div value="6">6</div>
<div value="7">7</div>
<div value="8">8</div>
<div value="9">9</div>
</div>
<div id="mark" class="center">
<div value="+">+</div>
<div value="-">-</div>
<div value="=">=</div>
</div>
</div>
<script type="text/javascript">
var num = document.getElementById('number');
var number = num.getElementsByTagName('div');
var mark = document.getElementById('mark').getElementsByTagName('div');
var screen = document.getElementById('screen');
var top1 = document.getElementById('top');
var bottom = document.getElementById('bottom');
addClick(number);
addClick(mark);
function addClick(obj) {
for (var i = 0; i < obj.length; i++) {
obj[i].onclick = function () {
if (this.innerHTML== '=') {
// = '9+9'
var str = bottom.innerHTML;
// eval()
var result = eval(str);
//
// top.innerHTML = str;
top1.innerHTML = bottom.innerHTML + '=';
//
bottom.innerHTML = result;
}else{
//
bottom.innerHTML += this.innerHTML;
}
}
}
// alert(number[i].getAttribute('value'));
}
function pro() {
// body...
}
</script>
</code></pre>
<br/>
</div>
</div>
</div>
</div>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.