在控制台中使用 JavaScript 获取用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16307415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Take user input with JavaScript in the console
提问by alt
I need to get user input when running a .js
in a console with spidermonkey like this:
.js
在使用spidermonkey的控制台中运行时,我需要获取用户输入,如下所示:
$ js myprogram.js
$ js myprogram.js
What's the JavaScript equivalent of Ruby's gets
?
Ruby 的 JavaScript 等价物是gets
什么?
回答by Yauhen Vasileusky
As far as I know, there is a readline() function, but it is a specific function for spidermonkey, it isn't a part of javascript.
据我所知,有一个 readline() 函数,但它是蜘蛛猴的特定函数,它不是 javascript 的一部分。
Example:
例子:
1)readline-test.js:
1)readline-test.js:
print("Type some text and press <ENTER>:\t");
var userInput = readline();
print("User input: " + userInput);
2)js readline-test.js
2)js readline-test.js
For more information see https://developer.mozilla.org/en-US/docs/SpiderMonkey/Introduction_to_the_JavaScript_shell.
有关更多信息,请参阅https://developer.mozilla.org/en-US/docs/SpiderMonkey/Introduction_to_the_JavaScript_shell。
回答by ars265
You can't really depend on the console being there. It's not something that is guaranteed, even while most browsers do have a javascript console. Beyond that I don't believe you can get values without calling a function with the console.
你不能真的依赖那里的控制台。即使大多数浏览器确实有一个 javascript 控制台,也不能保证这一点。除此之外,我不相信您可以在不使用控制台调用函数的情况下获取值。