网页设计-代码编辑器

如图,我想实现以下界面:

但是使用以下代码会出现很多问题:

<!DOCTYPE html>

<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style>
      #input {
              height: 50%;
              width: 98%;
               border: 1px solid #ccc;
               overflow: auto;
               background-color: white;
               position: absolute;
               top: 0px;
               left: 2%;
               }
            #line {
                height: 50%;
                 width: 2%;
                 size: 60px;
                 background-color: #f6f6f6;
                 position: absolute;
                 top: 0px;
                 left: 1px;
                 border: 1px solid #ccc;
                 overflow: auto;
                 text-align: right;
                    }
           #buttons {
              position: absolute;
              top: 53%;
              left: 0px;
              background-color: #f6f6f6;
              line-height: 50px;
                }
           .button {
              height: 40px;
              width: 150px;
              border: 1px solid #45d93c;
              color: black;
              background-color: #f6f6f6;
              font-size: 16px;
                 }
            #output {
               font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
               position: absolute;
               top: 53%;
               left: 15%;
               border: 2px solid #ccc;
               background-color: #fefefe;
               height: 28%;
               width: 85%;
             }
   </style>
</head>
<body>
   <pre id="line">1</pre>
    <pre id='input' contenteditable='true'>//main.js</pre>
    <div id="buttons">
       <button onclick='execute()' class='button'>运行 &rsaquo;</button>
       <br/>
       <button onclick='cler()' class='button'>清空</button>
       <br/>
       <button onclick="location.reload()" class='button'>刷新</button>
       <br/>
       <button class="button" click-type="copycode" data-clipboard-target="#output">复制结果</button>
    </div>
<div id='output'></div>
  <script>
function sam(str, n) {
  const parts = str.split('\n');
  return parts.slice(0, -n);
}
  document.getElementById("input").addEventListener('keydown', function(event){
      if(event.key==="Enter" || event.key==="Backspace"){
           var a=document.getElementById('line').innerHTML.split('\n').length;
           if(event.key==="Enter"){
              document.getElementById("line").innerHTML+='\n'+(a+1);
               } else if((document.getElementById('line').innerHTML.split('\n').length)>(document.getElementById('input').innerHTML.split('\n').length)){
            var c=document.getElementById('line').innerHTML;
            document.getElementById('line').innerHTML=c.substring(0, (c.length-1-(String(a).length)));
              }
     }
   });
   var module1 = document.getElementById('input'),module2 = document.getElementById('line');
              module1.addEventListener('scroll', function() {
                module2.scrollTop = module1.scrollTop;
                 });
             module2.addEventListener('scroll', function() {
                module1.scrollTop = module2.scrollTop;
                 });
         function execute(){
           document.getElementById('output').innerHTML = '';
           try {eval("const console={'log':function(ve){document.getElementById('output').innerHTML = document.getElementById('output').innerHTML+'&gt; '+ve+'<br/>'}};"+document.getElementById('input').innerHTML);}catch(e){document.getElementById('output').innerHTML = 'Error:'+e.message};
            }
        function cler(){
            document.getElementById('output').innerHTML = '';
                  }
 </script>
</body>
</html>

  • //后面的所有代码(包括换行后)的代码全部都会被忽略(已由gushi帖子 #4中解决 :white_check_mark:)
  • 一退格行数显示就出问题
  • 一换行就报错
  • 粘贴,剪切后不能更新行数

我认为你应当去除注释之后再eval

@gushi
怎么去注释

我再试一下

  var a=new Function(document.getElementById("input").innerHTML);
  a();
const fn = code => code.replace(/\/\/.*?\n/g, '\n').replace(/\/\*[\s\S]*?\*\//g, '');

好的

建议直接用VConsole

import('https://m.ccw.site/p01.js').then(()=>new VConsole)

想自己写…

。。。
html估摸着是这样

<textarea id="textarea" rows="1" cols="0" style="width: 100%;opacity: 0;"></textarea>
<div>
<div class="number">1</div><div class="row"></div><br class="br">
</div>

6,连个光标都没有