亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JQuery及AJAX實(shí)現(xiàn)名人名言隨機(jī)生成器

Original 2017-02-16 13:09:08 1375
abstract:這是我剛接觸AJAX的時候做的一個小應(yīng)用,主要功能如下:1.點(diǎn)擊按鈕可以隨機(jī)生成一句名人名言及其作者名字,如果沒有作者名字,則顯示“Unknown”。2.點(diǎn)擊按鈕可以把名人名言分享到推特或者微博。HTML:<div class="container-fluid text-center">  <h1>   

這是我剛接觸AJAX的時候做的一個小應(yīng)用,主要功能如下:

1.點(diǎn)擊按鈕可以隨機(jī)生成一句名人名言及其作者名字,如果沒有作者名字,則顯示“Unknown”。
2.點(diǎn)擊按鈕可以把名人名言分享到推特或者微博。

HTML:

<div class="container-fluid text-center">
 <h1>
  Random Quote Generator
 </h1>
 <div class="well quote-area">
  <span class="quote">
  </span>
  <span class="author">
  </span>
 </div>
 <div class="btns">
  <button class="btn btn-default btn-lg" id="tweet">
   <i class="fa fa-twitter" aria-hidden="true">
   </i>
    Tweet
  </button>
  <button class="btn btn-default btn-lg" id="weibo">
   <i class="fa fa-weibo" aria-hidden="true">
   </i>
    Weibo
  </button>
  <button class="btn btn-default btn-lg" id="change">
   <i class="fa fa-exchange" aria-hidden="true">
   </i>
    Get Quote
  </button>
 </div>
</div>
<footer class="text-center">
 Designed by
 <a href="http://blog.csdn.net/alenhhy" rel="external nofollow" target="_blank">
  Alen Hu
 </a>
</footer>

JQuery:

$(document).ready(function() {
 var quote, author;
  
 function getNewQuote() {
  $.ajax({
   type: "get",
   url: "http://api.forismatic.com/api/1.0/",
   jsonp: 'jsonp',
   dataType: 'jsonp',
   data: {
    method: 'getQuote',
    lang: 'en',
    format: 'jsonp'
   },
   success: function(response) {
    quote = response.quoteText;
    author = response.quoteAuthor;
    $('.quote').text('\"' + quote + '\"');
    if (author) {
     $('.author').text('by ' + author);
    } else {
     $('.author').text('by Unknown');
    }
   }
  });
 }
  
 getNewQuote();
  
 $('#change').on('click',
 function(event) {
  event.preventDefault();
  getNewQuote();
 });
  
 $('#tweet').on('click',
 function(event) {
  event.preventDefault();
  window.open('http://twitter.com/intent/tweet?text=' + encodeURIComponent(quote + ' by ' + author));
 });
  
 $('#weibo').on('click',
 function(event) {
  event.preventDefault();
  window.open('http://v.t.sina.com.cn/share/share.php?title=' + encodeURIComponent(quote + ' by ' + author));
 })
});

*forismatic的API可以獲取名人名言,但是只有英語和俄語版本的...不過中文類似的API也有很多的啦,實(shí)現(xiàn)原理都差不多。

更多關(guān)于JQuery及AJAX實(shí)現(xiàn)名人名言隨機(jī)生成器請關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!

Release Notes

Popular Entries