假终端浏览器 - 如何让它响应

时间:2017-02-24 20:49:18

标签: javascript jquery html

我正在尝试在我的浏览器中创建一个终端,我做了,但我不知道如何制作它,这样如果我输入"命令",它会响应某些内容"终端"。这是我的代码:

<body>
<div id="screen">$&gt;<input></input></div>
<script>
$("#screen input").focus();
$("#screen input").on('keydown', function(event) {
    if(event.which === 13) {// Enter key pressed
        var $this = $(this),
            val = $this.val();
        $this.focus().val('');
        if(val === "hello world") {
            //respond with something in the terminal here
        }
    }
});
</script>
</body>

所以我只是希望它在我执行命令时回复终端中的某些内容&#34; hello world&#34;。

2 个答案:

答案 0 :(得分:2)

您需要添加一个虚假的“控制台”输出区域。那么,这样的事情呢?

仅供参考:function myPrompt(arr) { var search = prompt("Please enter an integer to search for:"); document.getElementById("demo_c").innerHTML = "Search For: " + search; if(arr.indexOf(search) > - 1) { alert('element Found'); } } 应该是:$this.focus().val('');$this.val('');元素没有结束标记(<input>

</input>
// Get reference to the output area
var $out = $("#output");

$("#screen input").focus();
$("#screen input").on('keyup', function(event) {

    if(event.which === 13) {
      // Enter key pressed
      var $this = $(this);
      var val = $this.val();
      
      if(val === "hello world") {
        $out.append(">> Hello Galaxy<br>");
      }
      
      $this.val('');
    }
});
body {
  font-family:"courier new", monospaced;
  border-radius:5px;
  border:2px solid black;
  height:60vh;
  background-color:rgba(200,200,200, .5);
  padding:10px;
}

input { 
  background-color:rgb(0, 150, 0);
  color: #ff0;
  font-weight:bold;
  letter-spacing:.2em;
}
#output { 
  color:#000; 
  background-color:rgba(200,200,200, .25);
}

答案 1 :(得分:0)

您可以通过将字符串传递给.val()来设置输入的值:

&#13;
&#13;
    public static UpdateDefinition<TDocument> SetDictionary<TDocument, TKey, TValue>(this UpdateDefinition<TDocument> update, IDictionary<TKey, TValue> dict)
    {
        foreach (var field in dict)
        {
            update = update.Set(field.Key as String, field.Value);
        }

        return update;
    }
&#13;
&#13;
&#13;