【问题标题】:Twitter button not tweeting properlyTwitter按钮无法正确发推
【发布时间】:2017-06-09 22:17:44
【问题描述】:

我有一个随机报价生成器 (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);
  });

【问题讨论】:

标签: javascript jquery html twitter-bootstrap twitter


【解决方案1】:

您的代码中使用的链接是&lt;a&gt; 标记中的链接。

你不想这样。该链接没有传入任何数据,因此 twitter 将 URL 默认为 twitter.com/intent/tweet?url=&amp;original_referer=,这会将文本生成为您来自的 URL。 (在您的情况下,它是您本地的)。

由于您使用的是oncick() function.,因此无需将推特button&lt;a&gt; 标签包装起来

以下代码似乎可以完成您想要完成的工作。

首先修改你的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> 

你的.js 应该是这样的:

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]);
}

【讨论】:

  • 非常感谢。以一种简洁明了的方式帮助我解决了一段时间以来一直在努力解决的问题。
猜你喜欢
  • 2017-06-18
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 2013-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多