gpt4 book ai didi

jquery - Bootstrap 按钮按键不适用于 'Enter' 键

转载 作者:行者123 更新时间:2023-11-27 23:21:15 25 4
gpt4 key购买 nike

因此,我正在构建一个网站,该网站在按下按钮时随机打印数组中的引号。当一个人使用鼠标按下按钮时,它现在正在工作。但是,我添加了代码来监听“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 )]);    }    });

and your button into this

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

关于jquery - Bootstrap 按钮按键不适用于 'Enter' 键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com