【问题标题】:Undefined value when passing text to twitter web intent将文本传递给 twitter 网络意图时未定义的值
【发布时间】: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


    【解决方案1】:

    由于两次使用 var,您有 2 个不同的 result 变量。唯一获得值集的是getQuotes()中的本地值

    getQuotes() 之外声明的另一个总是未定义的。

    尝试改变

    var result = quotes[Math.floor(Math.random() * quotes.length)];
    

    result = quotes[Math.floor(Math.random() * quotes.length)];
    

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-03-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 2013-08-21
      • 2011-07-29
      相关资源
      最近更新 更多