javascript - 将文本传递到Twitter Web Intent时的未定义值

标签 javascript jquery css twitter

我有将引用(单击按钮时随机生成)传递给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/

相关文章:

javascript - 以编程方式单击单选输入时,jQuery 更改不会触发

jquery - jquery ui.autocomplete 的兼容性问题

html - Internet Explorer 8 中的焦点 CSS 标记

html - 使用变换 3d 和位置相对和绝对时居中背景图像

php - 像 Swoopo 或 Quibids 这样的网站如何让所有游戏玩家保持计时器

javascript - 创建一个动态扩展列数以适应屏幕大小的 html 表格

javascript - 完全坚持将 JQuery 加载到 WordPress 站点

javascript - Angular JS 类型错误 : $http is not a function

javascript - 如何使用 javascript 检测可见( View 中)元素

html - 如何用CSS放置背景图片