簡體   English   中英

將文本傳遞到Twitter Web Intent時的未定義值

[英]Undefined value when passing text to twitter web intent

我有將引用(單擊按鈕時隨機生成)傳遞給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

由於使用了兩次var result您有2個不同的result變量。 唯一獲得值集的是getQuotes()內部的值集

getQuotes()外部聲明的另一個始終是未定義的。

嘗試改變

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM