我正在使用这个:http://terminal.jcubic.pl/
我想输出一些东西,并且让那个输出成为一个超链接,当点击它时会输出其他东西。
jQuery(document).ready(function($) {
$('#terminal').terminal(function(cmd, term) {
term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
}, {
greetings: "Welcome!\n",
onInit: function(terminal) {
terminal.echo('Welcome!');
},
prompt: '> '
});
});
但是,
<a onClick="javascript:terminal.exec
不起作用,因为 terminal.exec 未在窗口的上下文中定义。正确的做法是什么?
谢谢!
最佳答案
您可以在 JavaScript 中定义 onclick
处理程序:
<!-- In your HTML -->
<a class="trigger-terminal" href="#">Terminal</a>
// In your JavaScript in the scope of terminal
$('.trigger-terminal').click(function() { terminal.exec(); });
或者您可以通过将terminal
变量附加到全局window
对象来公开您的terminal
变量:
window.terminal = terminal;
以下是与您的代码内联的两个建议:
// First suggestion
jQuery(document).ready(function($) {
$('#terminal').terminal(function(cmd, term) {
term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
}, {
greetings: "Welcome!\n",
onInit: function(terminal) {
$('.trigger-terminal').click(function() { terminal.exec(); });
terminal.echo('Welcome!');
},
prompt: '> '
});
// Second suggestion
jQuery(document).ready(function($) {
$('#terminal').terminal(function(cmd, term) {
term.echo('\nClick this: <a onClick="javascript:terminal.exec(\'somethingelse\')">somethingelse</a>', {raw: true});
}, {
greetings: "Welcome!\n",
onInit: function(terminal) {
window.terminal = terminal;
terminal.echo('Welcome!');
},
prompt: '> '
});
});
关于javascript - jQuery - 如何在另一个范围内调用一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30419916/