大家早上好,我知道之前已经问过这个问题,但作为一个我努力学习的Javascript Noob,我无法理解如何让我的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>
答案 0 :(得分:0)
您的链接目标是静态的,它始终是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])
}
答案 1 :(得分:0)
<span class="tweetquote" style="margin:15px; padding:15px; border:1px solid #ddd;">
<a href="https://twitter.com/intent/tweet?url=https://twitter.com&;text=Tweet This piece of content the Entire quote in content and its url&;via=twitter&;" title="Tweet This piece of content the Entire quote in content and its url" target="_blank" rel="noopener noreferrer">Tweet This piece of content the Entire quote in content and its url</a>
<a href="https://twitter.com/intent/tweet?url=https://twitter.com&;text=Tweet This piece of content the Entire quote in content and its url&;via=twitter&;" title="Tweet This piece of content the Entire quote in content and its url" target="blank" class="btn btn-twitter-"><i class="fa fa-twitter"></i>Tweet</a>