基于JQuery及AJAX實(shí)現(xiàn)名人名言隨機(jī)生成器
這是我剛接觸AJAX的時(shí)候做的一個(gè)小應(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 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可以獲取名人名言,但是只有英語(yǔ)和俄語(yǔ)版本的...不過中文類似的API也有很多的啦,實(shí)現(xiàn)原理都差不多。
DEMO在這兒,歡迎來FORK:Random Quote Generator。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 用JS生成UUID的方法實(shí)例
- Javascript生成全局唯一標(biāo)識(shí)符(GUID,UUID)的方法
- jQuery基于隨機(jī)數(shù)解決中午吃什么去哪吃問題示例
- jQuery實(shí)現(xiàn)的點(diǎn)贊隨機(jī)數(shù)字顯示動(dòng)畫效果(附在線演示與demo源碼下載)
- jQuery實(shí)現(xiàn)轉(zhuǎn)動(dòng)隨機(jī)數(shù)抽獎(jiǎng)效果的方法
- js jquery獲取隨機(jī)生成id的服務(wù)器控件的三種方法
- jquery輸入數(shù)字隨機(jī)抽獎(jiǎng)特效的簡(jiǎn)單實(shí)現(xiàn)代碼
- jQuery隨機(jī)密碼生成的方法
- jquery代碼實(shí)現(xiàn)簡(jiǎn)單的隨機(jī)圖片瀑布流效果
- jQuery 插件實(shí)現(xiàn)隨機(jī)自由彈跳氣泡樣式
- JS實(shí)現(xiàn)點(diǎn)擊生成UUID的方法完整實(shí)例【基于jQuery】
相關(guān)文章
jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕效果
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)簡(jiǎn)單彈幕效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11利用jquery實(shí)現(xiàn)下拉框的禁用與啟用
本文主要介紹了jquery實(shí)現(xiàn)下拉框的禁用與啟用的具體代碼。代碼清晰,容易理解。有需要的朋友可以參考下,希望會(huì)對(duì)大家有所幫助2016-12-12jQuery實(shí)現(xiàn)表格的增、刪、改操作示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)表格的增、刪、改操作,涉及基于jQuery的事件響應(yīng)及頁(yè)面元素動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01使用JQuery實(shí)現(xiàn)的分頁(yè)插件分享
本文給大家總結(jié)了幾種使用jQuery實(shí)現(xiàn)的分頁(yè)插件,效果非常不錯(cuò),有需要的小伙伴可以參考下。2015-11-11