像console.log那样制作一些历史源

时间:2016-07-14 12:56:20

标签: javascript developer-console

第一次询问Stack,有人可以帮我弄清楚如何在开发控制台之类的视觉反馈中输出eval,就像历史源一样。

(function init() {
var btn = document.getElementById("btn").onclick = function(){
            var iostore = document.getElementById("io");
            var history = document.getElementById("history");
            var result = eval(iostore.value);
            console.log(result);
            iostore.value = result;	
};


})();
body, html{width:100%;height:100%;margin: 0px auto;background-color:#EEE;}
#io{background-color:aqua;width:auto;height:auto;border:0;outline:none;font-size:1.5em;}
button{border:0;border-radius: 5px;background-color:#999;font-size:1.7em;}#history{resize: none;background-color:#EEE;border:0;outline:none;cursor:default;color:black;opacity:0.7;}#creation{background-color:aqua;width:auto;height:auto;border:0;outline:none;font-size:1.5em;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="interfacecontainer">
<input type="text" id="io">  
<button id="btn">run</button><br>
<textarea id="history"></textarea> 
<div id="creation"></div>
</div>   
</body>
</html>

What i mean (browser print-screen)

提前谢谢。

1 个答案:

答案 0 :(得分:0)

首先仔细阅读with_items loop确切了解您想要的内容。然后因为它试图评估一个表达式我将它放在try catch中,如果有异常,你可以输出异常。然后在textarea中附加结果或异常。我还调整了你的textarea的大小,以便你可以看到更多的结果。

(function init() {
var btn = document.getElementById("btn").onclick = function(){
            var iostore = document.getElementById("io");
            var history = document.getElementById("history");
try {
var result =eval(iostore.value);
history.value += result+"\n";
} catch (e) {
    iostore.value=e.message;
history.value += e.message+"\n";

}	
};


})();
body, html{width:100%;height:100%;margin: 0px auto;background-color:#EEE;}
#io{background-color:aqua;width:auto;height:auto;border:0;outline:none;font-size:1.5em;}
button{border:0;border-radius: 5px;background-color:#999;font-size:1.7em;}#history{resize: none;background-color:#EEE;border:0;outline:none;cursor:default;color:black;opacity:0.7;height:50px}#creation{background-color:aqua;width:auto;height:auto;border:0;outline:none;font-size:1.5em;}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div class="interfacecontainer">
<input type="text" id="io">  
<button id="btn">run</button><br>
<textarea id="history"></textarea> 
<div id="creation"></div>
</div>   
</body>
</html>