js 에서 eval () 방법 에 대해 계산기 의 작은 예 를 썼 다.

4655 단어
때로는 string 형식 으로 number 로 변환 할 수 있 지만 때로는 string 형식의 표현 식 을 얻 을 수 있 습 니 다. 그리고 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>

좋은 웹페이지 즐겨찾기