我有将引用(单击按钮时随机生成)传递给Twitter的JQuery代码。但是,当您打开新窗口来鸣叫报价时,它会显示与实际报价相反的undefined值。
这是代码:
$(document).ready(function(){
var result;
function getQuotes(){
var quotes = [ "Thousands of candles can be lighted from a single candle, and the life of the candle will not be shortened. Happiness never decreases by being shared.-\n Buddha",
"Happiness is the art of never holding in your mind the memory of any unpleasant thing that has passed.\n-Unknown",
"To be happy, we must not be too concerned with others.\n-Albert Camus",
"If you want happiness for an hour — take a nap.If you want happiness for a day — go fishing.If you want happiness for a year — inherit a fortune.If you want happiness for a lifetime — help someone else."]
var result = quotes[Math.floor(Math.random() * quotes.length)];
document.getElementById("stuff").innerHTML = result;
}
$("#tweet").on('click',function(){
window.open("http://www.twitter.com/intent/tweet?text="+result);
});
$(document).on('click', '#quotes', function() {
getQuotes();
});
});
这是指向代码笔的链接:http://codepen.io/sibraza/pen/KMPzxx?editors=1010
最佳答案
由于使用了两次result
,因此您有2个不同的var
变量。唯一获得值集的是getQuotes()
内部的值集
在getQuotes()
之外声明的另一个始终未定义。
尝试改变
var result = quotes[Math.floor(Math.random() * quotes.length)];
至
result = quotes[Math.floor(Math.random() * quotes.length)];
关于javascript - 将文本传递到Twitter Web Intent时的未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533840/