2016-08-27 12 views
-1

私はFreeCodeCamp用のランダムなクォートジェネレータを作っています。私はツイートボタンを機能させる助けが必要です。私の引用符は配列として文字列として格納されます。 href = 'http://twitter.com/home/?status=stringのように、hrefの最後に追加するには離れていますか?hrefの最後に文字列を追加できますか?

http://codepen.io/tyl-er/pen/KrjWVA?editors=1000

<head> 
    <script src="https://use.fontawesome.com/41c0e08ce5.js"></script> 
</head> 

<div class="flip"> 
    <div onclick="randomIndex" class="card"> 
     <div class="face front"> 
      <!--logo here--> 
     </div> 
     <div class="face back"> 

      <textarea rows="10" cols="30" type=text id="mytext"></textarea> 
     </div> 
    </div> 
</div> 

<div class="div10"> 
    <p>(Spoliers maybe ¯\_(ツ)_/¯) <del>Written</del> and coded by Tyler Pelzer</p> 
    <a class="btn btn-social-icon btn-twitter" target="blank" href='http://twitter.com/home/?status=Twitter is like the lunch meeting with potential clients before you do the pitch. via @blogtyrant'> 
     <span class="fa fa-twitter"></span> 
    </a> 
</div> 


$('.flip').click(function printquote(){ 

var strings = []; // <--superlong array of quotes 
var randomIndex = Math.floor(Math.random() * strings.length); 
var randomString = strings[randomIndex]; 
document.getElementById("mytext").value = randomString; 
//that was easy^ 

}); 

$('.flip').click(function(){ 
     $(this).find('.card').addClass('flipped').mouseleave(function() 
     { 
      $(this).removeClass('flipped'); 

     }); 
     return false; 
}); 
+0

空の配列ではなく文字列の例を与えるようにしてください。 'http://twitter.com/home/?status = string'の場合、' string'だけの文字列か 'status = string'ですか?それがちょうど 'string'の場合、多くの配列の値に定義されているキーはどこですか? –

+0

これは、「愛によってあなたは食べることができず、働くことができない大きな稲妻を意味し、結婚して赤ちゃんを作るだけです。あなたが愛しているのは、私のような人が...ナイロンを売るために発明したものだよ - ドン " 非常に長い配列なので、ちょうど短縮された。 –

答えて

0

あなたはのhref属性にあなたがテキストエリアの値を設定したのと同じ方法を設定することができます。リンクタグにIDを追加し、:

​​

編集:あなたはフリップクリックハンドラとは別にこれをしたいと思って、以下のコードリファクタリングしましょう:この中へ

$('.flip').click(function printquote(){ 
    var strings = []; // <--superlong array of quotes 
    var randomIndex = Math.floor(Math.random() * strings.length); 
    var randomString = strings[randomIndex]; 
    document.getElementById("mytext").value = randomString; 
    //that was easy^ 
}); 

を:

function getQuotes() { 
    return []; // <--superlong array of quotes 
} 

function getRandomEntry(entries) { 
    return entries[Math.floor(Math.random() * entries.length)]; 
} 

$('.flip').click(function printquote(){ 
    document.getElementById("mytext").value = getRandomEntry(getQuotes()); 
    //that was easy^ 
}); 

あなたは、この新しいコードでランダムな引用を取得するための機能を共有することができますこの方法:

$(document).ready(function() { 
    var randomQuote = getRandomEntry(getQuotes()); 
    document.getElementById('twitterLink').href = 'https://twitter.com/home?status=' + encodeURIComponent(randomQuote); 
}); 
+0

ありがとう、私はgetElementByhrefを使用しようとしていましたが、私はちょうどそれを作ったので、うまくいきませんでした。私のhrefは何ですか? –

+0

答えには、「簡単でした」の下の.flipのクリックハンドラ内に配置できるものがあります:) – jedifans

+0

私はちょうど.flipでカードを反転させたいと思います。私はツイッターリンクのための別個のブートストラップボタンを持っています。私はこれをやってみたが、うまくいかなかった。 $( '#twitterLink')(関数(){ \tのdocument.getElementById( 'twitterLink')をクリックHREF = 'https://twitter.com/home?status=' + encodeURIComponentで(randomString);。。 }; –

関連する問題