2

我有疑问,如何在 Jquery 终端中添加价值?像这样的东西:

jQuery(function($, undefined) {
 $('#terminal').terminal(function(command) {
     if(command == 'test ' + value) { // Value (something a user wrote)
        alert(value);
     }else{
     alert("You don't wrote anything."); 
     }

 }, {
 greetings: 'Hello.',
 name: 'Name',
 prompt: 'Terminal> ',
 color: 'some color'
 });
});

我的意思是这样的:

if(command == 'you type ' + userText) { 
alert(userText); 
} else { 
alert("You don't wrote anything."); 
}
4

1 回答 1

0

如果 value 是先前的命令,您可以做两件事,您可以将其保存在 variabe 中,如下所示:

jQuery(function($, undefined) {
    var prev_command = '';
    $('#terminal').terminal(function(command) {
        if (command == 'test ' + prev_command) {
           alert(value);
        } else {
           alert("You don't wrote anything."); 
        }
        prev_command = command;
    }, {
       greetings: 'Hello.',
       name: 'Name',
       prompt: 'Terminal> '
  });
});

或者您可以使用推送:

jQuery(function($, undefined) {
   $('#terminal').terminal(function(command) {
      var value = command;
      this.push(function(command) {
        if (command == 'test ' + value) {
           alert(value);
        } else {
           alert("You don't wrote anything."); 
        }
        term.pop();
      });
   }, {
      greetings: 'Hello.',
      name: 'Name',
      prompt: 'Terminal> '
   });
});
于 2018-06-07T17:55:06.123 回答