将插入符号放在以前的可编辑 div 中并按 Enter 应重新评估该输入

我有这个在线代码执行可编辑 div,它目前通过使用名为 parse 的函数逐步工作。您输入一些东西(例如 7+5)并按回车键,您会在下面的新行上得到结果并创建一个新的输入 div,但是如果您返回到之前的可编辑输入 div 并输入一个新的输入并按回车键它无法正常工作,因为默认情况下会创建新的输出和输入 div,并且不会重新评估新代码。


请注意,如果您在输入框(可编辑的 div)中的输入末尾放入 : 则不会创建新的输出 div。输出在视图中隐藏。这是为了避免不需要显示的长输出而设计的。


我还尝试编写一个新函数 parse2() 但随后逐步评估停止工作。让两者(逐步返回到旧的可编辑 div 并重新评估)同时工作似乎非常困难。如何才能做到这一点?我想我需要在函数 parse() 中使用一些 if 语句来确定它是正在执行的新输入代码还是正在重新评估的旧输入代码。我也尝试为输入 div 分配一个数字,但我没有设法让它工作。


 function parse2(e) {

 if (e.keyCode == 13) {

 event.preventDefault();


 if (document.getElementById("output") == null) {

  CreateOutputDiv();

  CreateInputDiv(); 

  // calculates and assign values to output div 

  var d1 = document.getElementById(JSON.stringify(input)).innerText; 

  console.log("in = " + d1); 

  var d2 = eval(d1);

  console.log("out = " + d2);

  output.innerHTML = d2;

  document.getElementById("count").value += '\n' + '\n' + cc;     

  } else if { //re-evaluates inputbox = works

  var d1 = document.getElementById(JSON.stringify(input)).innerText;  

  console.log("new in = " + d1); 

  var d2 = eval(d1);

  console.log("new out = " + d2);

  output.innerHTML = d2; 

  input.focus();

   }  else { //re-evaluates inputbox = works

  CreateOutputDiv();

  var d1 = document.getElementById(JSON.stringify(input)).innerText;  

  console.log("new in = " + d1); 

  var d2 = eval(d1);

  console.log("new out = " + d2);

  output.innerHTML = d2; 

  input.focus(); }}}


犯罪嫌疑人X
浏览 136回答 1
1回答

回首忆惘然

当您创建新的输入和输出 div 时,您将对所有输入和输出应用相同的 id。取而代之的是,您可以使用计数器编号“cc”来提供所有唯一 ID。然后,当您调用 parse 函数时,您已经在为其提供参数“this”。使用 'this' 通过 id 获取元素。如果具有此输出 id 的 div 已经存在,只需更改其内容,否则创建一个新的。&nbsp; &nbsp; <script>&nbsp; &nbsp; // counts the number of input divs created&nbsp; &nbsp; function increment() {&nbsp; &nbsp; &nbsp; increment.n = increment.n || 0;&nbsp; &nbsp; &nbsp; return ++increment.n;&nbsp; &nbsp; }&nbsp; &nbsp; // creates an input div&nbsp; &nbsp; function CreateInputDiv() {&nbsp; &nbsp; &nbsp; increment();&nbsp; &nbsp; &nbsp; cc = increment.n;&nbsp; &nbsp; &nbsp; //console.log("increment.n = " + cc);&nbsp; &nbsp; &nbsp; input = document.createElement("div");&nbsp; &nbsp; &nbsp; input.setAttribute("id", "input"+cc);&nbsp; &nbsp; &nbsp; input.setAttribute("class", "input");&nbsp; &nbsp; &nbsp; input.innerHTML = "&nbsp";&nbsp; &nbsp; &nbsp; input.setAttribute("contenteditable", "true");&nbsp; &nbsp; &nbsp; input.setAttribute("onkeypress", "parse(event, this)");&nbsp; &nbsp; &nbsp; document.getElementById('calc').appendChild(input);&nbsp; &nbsp; &nbsp; input.focus();&nbsp; &nbsp; }&nbsp; &nbsp; // creates an output div&nbsp;&nbsp; &nbsp; function CreateOutputDiv() {&nbsp; &nbsp; &nbsp; output = document.createElement("div");&nbsp; &nbsp; &nbsp; output.setAttribute("id", "output"+cc);&nbsp; &nbsp; &nbsp; output.setAttribute("class", "output");&nbsp; &nbsp; &nbsp; output.setAttribute("tabindex", "0");&nbsp; &nbsp; &nbsp; output.setAttribute("contenteditable", "true");&nbsp; &nbsp; &nbsp; document.getElementById('calc').appendChild(output);&nbsp; &nbsp; }&nbsp; &nbsp; function parse(e,e2) {&nbsp; &nbsp; &nbsp; &nbsp; //console.log(e2);&nbsp; &nbsp; &nbsp; var key = window.event.keyCode;&nbsp; &nbsp; &nbsp; if (key == 13) { //keycode for enter&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; //console.log(e2.id);&nbsp; &nbsp; &nbsp; &nbsp; var inId = e2.id;&nbsp; &nbsp; &nbsp; &nbsp; var outId = "output"+ inId.substring(5);&nbsp; &nbsp; &nbsp; &nbsp; //console.log(outId);&nbsp; &nbsp; &nbsp; &nbsp; var inz = input.innerText;&nbsp; &nbsp; &nbsp; &nbsp; // check if input contains a colon. Hides output if colon exist.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (inz.indexOf(':') > -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// colon&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var inz = input.innerText.replace(/:/g, '');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("input without colon = " + inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var outz = eval(inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("out = " + outz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("count").value += '\n' + eval(cc + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateInputDiv();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // no colon = display output&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // counter&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(document.getElementById(outId)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("Already created");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inz = document.getElementById(inId).innerText;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var outz = eval(inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById(outId).innerHTML = outz;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("count").value += '\n' + '\n' + eval(cc + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // create output div&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateOutputDiv();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // calculate and assign output value to output div&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("input = " + inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var outz = eval(inz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //console.log("out = " + outz);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.innerHTML = outz;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // creates a new input div&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CreateInputDiv();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript