我有一个随机引号生成器(http://codepen.io/anon/pen/wgqWgo),当我尝试推特引用时,我得到以下结果:

在我的职责中,我没有正确地链接我的引文。作为JavaScript的新手,很难说到底是什么。我对此进行了广泛的搜索,似乎找不到解决办法。
我的HTML:
<div class="row">
<div class="col-12 buttons">
<a class="twitter-share-button" href="http://twitter.com/share" target="_blank"><button type="button" class="btn btn-outline-danger" onclick="tweetIt()"><i class="fa fa-twitter" aria-hidden="true"></i></button></a>
<button type="button" class="btn btn-outline-danger" onclick="newQuote()">Quote</button>
</div>
</div>
<div class="row">
<div class="col-12">
<div id="quoteDisplay" class="writing">
<!-- Quotes here -->
</div>
</div>
</div> 我的JavaScript:
var quotes = [
"There is nothing to writing. All you do is sit down at a typewriter and bleed.",
"Happiness in intelligent people is the rarest thing I know.",
"The world breaks everyone, and afterward, some are strong at the broken places."
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
};
$(".twitter-share-button").click(function(){
$(this).attr("href", 'https://twitter.com/intent/tweet?text=' + randomNumber);
});发布于 2017-01-24 22:51:15
代码中使用的链接是<a>标记中的链接。
你不会想要那样的。该链接没有数据被传递,因此twitter是默认的URL作为twitter.com/intent/tweet?url=&original_referer=,它生成文本作为您来自的URL。(就你而言,这是你的本地人)。
没有必要将twitter button包装为<a>标记,因为您使用的是oncick() function.
下面的代码似乎完成了您想要完成的任务。
HTML 首先修改,如下所示:
<div class="row">
<div class="col-12 buttons">
<button type="button" class="btn btn-outline-danger" onclick="tweetIt()">Tweet</button>
<button type="button" class="btn btn-outline-danger" onclick="newQuote()">Quote</button>
</div>
</div>
<div class="row">
<div class="col-12">
<div id="quoteDisplay" class="writing">
<!-- Quotes here -->
</div>
</div>
</div> 和您的应该如下所示:
var quotes = [
"There is nothing to writing. All you do is sit down at a typewriter and bleed.",
"Happiness in intelligent people is the rarest thing I know.",
"The world breaks everyone, and afterward, some are strong at the broken places."
]
function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quoteDisplay').innerHTML = quotes[randomNumber];
};
function tweetIt(){
var randomNumber = Math.floor(Math.random() * (quotes.length));
window.open("https://twitter.com/intent/tweet?text=" + quotes[randomNumber]);
}https://stackoverflow.com/questions/41839658
复制相似问题