2016-09-26 8 views
1

だから、うまく動作するランダムなクォートジェネレータを作った。私はランダムに生成された見積もりをとり、それをツイートにしたいと考えています。私はテキストをつぶす方法を知っています。私の問題は、ランダムな見積もりを生成する関数を取り、ここでつぶやくへのURLに追加されたことは私のコードです:Twitter URLのリンクURLに変数として関数を追加する方法は?

 <h1>Random Quote Generator</h1> 
       <p>Star Wars Quotes</p> 
       <button class="btn btn-default" type="submit">New Quote</button>   <div class="row"> 
     <div class="quote-box col-md-6 col-md-offset-3"> 
     <p class="quote">"Placeholder Quote"</p> 
     <p class="author">Placheolder Author</p> 
     <a onclick="getTweet();" href="#" target="_blank"><img src="http://www.iconsplace.com/icons/preview/yellow/twitter-2-256.png" alt="twitter logo"> Use the Force to Tweet</a> 
     </div><!--quote-box--> 
    </div><!--row--> 


    $(document).ready(function() { //random quote works fine 
     function pickQuote() { 
      var quotes = ["It's a trap!", "Mmm. Lost a planet, Master Obi-Wan has. How embarrassing.", "I find your lack of faith disturbing"]; 
      var author = ["-Admiral Akbar", "-Yoda", "-Darth Vader"]; 

      var randomNum = Math.floor((Math.random() * quotes.length)); 

      var randomQuote = quotes[randomNum]; 

      var randomAuthor = author[randomNum]; 

     $(".quote").text(randomQuote); 
     $(".author").text(randomAuthor); 
     } 

     $(".btn").on("click", function() { 
     pickQuote(); 
     }); 

    //this is where the tweet magic is supposed to happen 
    var a = pickQuote(); 
    function getTweet(){ 
     return "http://twitter.com/home/?status=" + a; 
    } 


}); 

任意の助けをいただければ幸いしてください。

答えて

0

 var pickQuote = function() { 
 
     \t var quotes = ["It's a trap!", "Mmm. Lost a planet, Master Obi-Wan has. How embarrassing.", "I find your lack of faith disturbing"]; 
 
      var author = ["-Admiral Akbar", "-Yoda", "-Darth Vader"]; 
 

 
      var randomNum = Math.floor((Math.random() * quotes.length)); 
 

 
      var randomQuote = quotes[randomNum]; 
 

 
      var randomAuthor = author[randomNum]; 
 

 
      $(".quote").text(randomQuote); 
 
      $(".author").text(randomAuthor); 
 
      return randomQuote + " " + randomAuthor; 
 
     } 
 

 
     
 
     var getTweet = function() { 
 
     \t var a = pickQuote(); 
 
      var url = "http://twitter.com/home/?status=" + a; 
 
      console.log(url); 
 
      return url; 
 
     } 
 
     
 
     $(document).ready(function() { 
 
     \t $(".btn").on("click", function() { 
 
     \t \t pickQuote(); 
 
      }); 
 
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="row"> 
 
     <div class="quote-box col-md-6 col-md-offset-3"> 
 
     <p class="quote">Placeholder Quote </p> 
 
     <p class="author">Placheolder Author </p> 
 
     <a onclick="getTweet();" href="#" ><img src="http://www.iconsplace.com/icons/preview/yellow/twitter-2-256.png" alt="twitter logo"> Use the Force to Tweet</a> 
 
     </div><!--quote-box--> 
 
    </div><!--row-->

+0

おかげで多分私は、HTMLの多くを追加している必要があります。ボタン(.btn)は引用符をランダム化し、引用符付きのhtmlがhtmlの前にhtmlをもう一度チェックする前にhtmlにあります。そのdivの前の行に追加して、鮮明な画像を得ることができます – MikeL5799

0

.button { 
 
    background-color: white; 
 
    color: black; 
 
    border: 2px solid #555555; 
 
    text-decoration: none; 
 
    display: inline-block; 
 
    font-size: 16px; 
 
    -webkit-transition-duration: 0.4s; 
 
    /* Safari */ 
 

 
    transition-duration: 0.4s; 
 
    cursor: pointer; 
 
    height: 30%; 
 
    vertical-align: top;text-align: left; 
 
} 
 
.button:hover { 
 
    background-color: #555555; 
 
    color: white; 
 
} 
 
#grid { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    background-color: #ffffff; 
 
    display: flex; 
 
    flex: 1; 
 
} 
 
#gridli { 
 
    width: 24%; 
 
    margin-left: 0.5%; 
 
    margin-right: 0.5%; 
 
}
<head> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
     </head> 
 
     
 
     <body> 
 
    
 

 
<ul id="grid"> 
 
    <li id="gridli"> 
 
     <form action="http://google.com"> 
 
     <button class="button" type="submit" /> 
 
     Computers, printers, mobile phones and the widgets that accompany them account for the emission of about two per cent of the estimated total of emissions from human activity. And that is the same as the aviation industry&#8217;s contribution. 25 per cent of the emissions in question are generated by the manufacture of computers. The rest come from their use. 
 
     </button> 
 
     </form> 
 
    </li> 
 
    <li id="gridli"> 
 
     <form action="http://google.com" style="float: left"> 
 
     <button class="button" type="submit" /> 
 
     Computers can be used to reduce emissions produced by other industries, up to 7.8 billion tonnes by 2020, or five times ICT&#8217;s own footprint. Computers can make industries more efficient and less wasteful of power and fuel. 
 
     </button> 
 
     </form> 
 
    </li> 
 
    <li id="gridli"> 
 
     <form action="http://google.com" style="float: left"> 
 
     <button class="button" type="submit" /> 
 
     The use of computers has led to both positive and negative impacts on the climate change of our planet. Computers can help make some processes more efficient and save energy while the use and manufacture of computers contributes to the increased use of energy which leads to global warming. This website helps you to look at the positives and negatives of computing and climate change.</p> 
 
     </button> 
 
     </form> 
 
    </li> 
 
    <li id="gridli"> 
 
     <form action="http://google.com" style="float: left"> 
 
     <button class="button" type="submit" /> 
 
      <h3> Saving Energy</h3> 
 
      There are many ways in which computers can help cut down on our use of energy. Computers are steadily improving the amount of energy that they use with the smaller computers producing less CO2 than larger desktop computers. 
 
     </button> 
 
     </form> 
 
    </li> 
 
</ul>

関連する問題