Bootstrap 按钮按键不适用于 'Enter' 键
Bootstrap button keypress not working for 'Enter' key
所以我正在构建一个网站,当按下按钮时,它会随机打印数组中的引号。当一个人使用鼠标按下按钮时,它现在正在工作。但是,我添加了代码来侦听 '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
我认为你需要改变你的 keydown 侦听器
进入这个
$('#newQuote').keyup(function(event){
if(event.keyCode == 13){
$('blockquote').text(quotes[Math.floor( Math.random() * quotes.length )]);
}
});
把你的按钮放进去
<button id="newQuote" class="btn btn-primary btn-lg">New Quote</button>
所以我正在构建一个网站,当按下按钮时,它会随机打印数组中的引号。当一个人使用鼠标按下按钮时,它现在正在工作。但是,我添加了代码来侦听 '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
我认为你需要改变你的 keydown 侦听器 进入这个
$('#newQuote').keyup(function(event){ if(event.keyCode == 13){ $('blockquote').text(quotes[Math.floor( Math.random() * quotes.length )]); } });
把你的按钮放进去
<button id="newQuote" class="btn btn-primary btn-lg">New Quote</button>