gpt4 book ai didi

javascript - 允许我的推文按钮分享随机引用

转载 作者:行者123 更新时间:2023-11-28 04:13:20 26 4
gpt4 key购买 nike

大家早上好,我知道这个问题之前已经被问过,但作为一个正在努力学习的 Javascript 菜鸟,我无法弄清楚如何让我的 Twitter 按钮来分享随机引用。如果可能的话,请一位好心的开发人员向我解释一下如何实际输入它。我对 HTML、CSS 和 Bootstrap 有很好的理解,但希望有人向我解释这一点。

提前非常感谢。

var quotes = [
"\'It is never too late to be what you might have been.\' - George Eliot",
"\'What lies behind us and what lies before us are tiny matters compared to what lies within us.\' - Henry Stanley Haskins",
"\'Great thoughts speak only to the thoughtful mind, but great actions speak to all mankind.\' - Emily P. Bissell",
"\'I haven’t failed. I’ve just found 10,000 ways that don’t work.\' - Thomas Edison",
"\'In between goals is a thing called life, that has to be lived and enjoyed.\' - Sid Caesar"
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
}
<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<body>


<h1>Simple Quote Generator</h1>
<div id="quoteDisplay">
</div>

<div class="quoteButton">
<button onclick="newQuote()">New Quote</button>

</div>
<a href="http://twitter.com/intent/tweet/?text" title="Share on Twitter" target="blank" class="btn btn-twitter-"><i class="fa fa-twitter"></i> Twitter</a>
<script src="javascript.js"></script>
</body>
</html>

最佳答案

您的链接目标是静态的,始终为 twitter.com/intent/tweet/?text。我不熟悉 Twitter API,但我猜它期望 URL 末尾有类似 ?text=some&encoded&text 的内容。您需要设置文本的值:

<a href="" id="twitterLink">

function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];

document.getElementById('twitterLink').href = "http://twitter.com/intent/tweet/?text=" + encodeURIComponent(quotes[randomNumber])

}

关于javascript - 允许我的推文按钮分享随机引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46072044/

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