我已经编写了一个随机报价生成器,并希望能够将报价分享到 Twitter。 http://codepen.io/nachamuami/pen/KrRYpY 通过尝试向该函数发送推文,它会将整个函数调用到 Twitter,而不是代码生成的引用。任何指示将不胜感激。
function getQuote(){
var arrayLength = quoteArray.length; //number of entries in array
var randomValue = Math.floor(Math.random()*arrayLength);
var newQuoteY = quoteArray[randomValue].yiddish;
var newQuoteE = quoteArray[randomValue].translation;
$('#inYiddish').html(newQuoteY);
$('#inEnglish').html(newQuoteE);};
$('#quote-button').click(function(){
getQuote();
});
$('#twitter-button').click(function(){
window.open("https://twitter.com/intent/tweet?text=" + getQuote);
});
});
最佳答案
声明
window.open("https://twitter.com/intent/tweet?text=" + getQuote);
实际上是将函数本身附加到 URI 中。 JavaScript 通过采用字符串形式进行补偿,在本例中,它或多或少与您为 getQuote
所做的声明完全相同,尽管是一个字符串。
您的意思可能如下:
window.open("https://twitter.com/intent/tweet?text=" + getQuote());
在这种情况下,您仍然会遇到错误,因为 getQuote
没有返回值,因此推文只会显示 undefined
。要纠正此问题,您需要在 getQuote
中返回一个字符串。或者,您可以在 getQuote
外部声明 newQuoteY
和 newQuoteE
,但仍由其设置。这将允许类似下面的内容,其中当前显示的意第绪语引用被发布到推特上。
window.open("https://twitter.com/intent/tweet?text=" + newQuoteY);
关于javascript - 推文引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39627173/