【发布时间】:2016-09-28 18:23:12
【问题描述】:
我有一个 JQuery 代码,它可以将一个引用(单击按钮时随机生成)传递给 twitter。但是,当您打开新窗口来推文引用时,它会显示一个与实际引用相反的 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();
});
});
这里是codepen的链接:http://codepen.io/sibraza/pen/KMPzxx?editors=1010
【问题讨论】:
标签: javascript jquery css twitter