我正在尝试使用:http://terminal.jcubic.pl/
我希望能够从另一个函数调用 term.echo 以便能够将数据放入终端内,但我无法引用它。
下面是代码:
jQuery(document).ready(function($) {
var id = 1;
$('body').terminal(function(command, term) {
if (command == 'help') {
term.echo("available commands are mysql, js, test");
} else{
term.echo("entered: " + command);
}
}, {
greetings: "Shell",
onBlur: function() {
return false;
}
});
});
如何从外部访问 term.echo,以便我可以通过单击按钮调用 term.echo 来添加数据?
最佳答案
最简单的方法是使用全局变量作为term对象的引用。在您的示例中,可能如下所示:
// global placeholder for term object
var terminal = null;
jQuery(document).ready(function($) {
var id = 1;
$('body').terminal(function(command, term) {
// setting up global reference
terminal = term;
if (command == 'help') {
term.echo("available commands are mysql, js, test");
} else{
term.echo("entered: " + command);
}
}, {
greetings: "Shell",
onBlur: function() {
return false;
}
});
});
此代码的问题在于,它会在 document 上触发 ready 事件后加载终端,而您永远不确定何时会发生这种情况。
在 document.ready 触发后,您将可以在任何地方使用 terminal.echo('');
。
关于javascript - JQuery Terminal term.echo 来自另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15715469/