gpt4 book ai didi

javascript - 在 Jquery 中单击推文按钮发送随机报价

转载 作者:行者123 更新时间:2023-11-30 08:32:31 25 4
gpt4 key购买 nike

我正在做一个“随机报价机”,它从存储的数组中随机引用并让用户发推文。我已经创建了随机报价生成器,但无法发布相同的推文。需要有关推文按钮的帮助。

我的 Jquery

var quote = new Array()
quote[0] = 'this is first quote'
quote[1] = 'this is second quote'

function tweet() {
window.open('https://twitter.com/intent/tweet?hashtags= freecodecamp&text=' + encodeURIComponent($(".message").html(quote[randomquote]));
}


$(document).ready(function() {
$("#getMessage").on("click", function(){
var randomquote = Math.floor(Math.random()*(quote.length-1));
$(".message").html(quote[randomquote]);
});

$('#twitter-share-button').on('click', tweet)

})

一些html

<div class = "col-xs-12 well message">
<h3>Click to get Random Quotes</h3>
</div>
<button id = "tweetMessage" class = "btn btn-default btn-lg" target="_blank"><i class="fa fa-twitter fa-fw"></i> Tweet it</button>
<button id = "getMessage" class = "btn btn-default btn-lg"><i class="fa fa-fire fa-fw"></i> Get Quote</button>

最佳答案

  $('#twitter-share-button').on('click', tweet)

您没有包含 id="twitter-share-button" 的元素

也不要在获得随机数时减去 1。由于您正在执行 Math.floor 并且由于 Math.random 永远不会恰好返回 1,因此它永远不会达到上限。

此外,您的 randomquote 变量在 tweet 函数内部使用,但仅存在于您的 document.ready block 中。您需要想出一种方法来传递它。更有意义的是传递消息本身而不是索引。

同样对于文本,你应该使用 .text(...) 而不是 .html(...) 因为这将有助于安全(查找交叉如果您有兴趣,可以编写网站脚本)。

我看到你在做 freecodecamp。酷并保持下去!

关于javascript - 在 Jquery 中单击推文按钮发送随机报价,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699577/

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