Bootstrap按钮按键不能用于“Enter”键

时间:2016-12-04 04:58:21

标签: jquery html twitter-bootstrap keypress enter

所以我正在构建一个网站,当按下按钮时,该网站会随机打印数组中的引号。当用鼠标按下按钮时,它正在工作。但是,我添加了代码来监听“Enter”键以单击所述按钮,但它无效。我今天已经玩了很长时间的代码了,我仍然无法让它工作。这是我现在的代码:

JQuery的:

$(document).ready(function(){
 var quotes = ['"A dream does not become reality through magic; it takes sweat, determination, and hard work." - Colin Powell', 
'"Success is the result of perfection, hard work, learning from failure, loyalty, and persistence." - Colin Powell', 
'"Nothing ever comes to one, that is worth having, except as a result of hard work." - Booker T. Washington', 
'"I learned the value of hard work by working hard" - Margaret Mead', 
'"Out work everyone. If you are working harder than everybody, then what are you worrying about?" - John Sonmez', 
'"Those dreams that you have, those goals that you have, do not put them off another second. Get on them. Go out and make them happen starting right now" - Jocko Willink', 
'"Go to bed tired." - Tim Kennedy', 
'"Do that thing that you dont know if you can do. Stop being afraid of hard work." - Tim Kennedy', 
'"Discipline equals freedom. The more discipline you have, the harder you work, the more effort you put into it, the more freedom you are going to have." - Jocko Willink',
'"Dont think about it, start doing it." - Anonymous'];

var rand = quotes[Math.floor( Math.random() * quotes.length )];
 $('blockquote').text(rand); 

//On Button Click 
$('#newQuote').click(function(){
  $('blockquote').text(quotes[Math.floor(Math.random() * quotes.length)]);
});

//Listen for Enter Key
$('blockquote').keyup(function(event){
if(event.keyCode == 13){
    $('#newQuote').click();
    $('blockquote').text(quotes[Math.floor(Math.random() * quotes.length)]);
 }

  });
});

我的HTML:

<a id ="newQuote" class="btn btn-primary btn-lg">New Quote</a>

此处是codepen上的网站:http://codepen.io/codyreandeau/pen/VmzWXG

0 个答案:

没有答案