从一个文本框执行代码,并在HTML中的另一个文本框中显示输出 [英] Execute code from one textbox and display the output in another textbox in HTML

查看:86
本文介绍了从一个文本框执行代码,并在HTML中的另一个文本框中显示输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两个文本框,在第一个文本框中我必须编写我的JAVASCRIPT代码,当我点击按钮时,想要在我的第二个文本框中输出。我使用了以下方法执行代码,但整个页面正在重新加载并显示输出。

Having two textbox, in first text box i have to write my JAVASCRIPT code and when i click button, want my output in my second text box. I had used the following method its executing the code but the entire page is getting reload and output is getting displayed.

<script>
function doIt() {
    var headID = document.getElementsByTagName("head")[0];
    var newScript = document.createElement("script");
    newScript.type = "text/javascript";
    newScript.innerHTML = document.getElementById("textarea").value;
    headID.appendChild(newScript);
}
</script>


推荐答案

您无需在节点中插入节点body为了执行JS。相反,您可以使用eval命令。你可以做这样的事情

You don't need to insert a node into the body in order to execute the JS. Instead you can just use eval command. You could do something like this

jsVal = document.getElementById("textarea").value;
document.getElementById("textarea2").value = eval(jsVal);

这篇关于从一个文本框执行代码,并在HTML中的另一个文本框中显示输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆