基于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可以獲取名人名言,但是只有英語和俄語版本的...不過中文類似的API也有很多的啦,實(shí)現(xiàn)原理都差不多。
DEMO在這兒,歡迎來FORK:Random Quote Generator。
以上就是本文的全部內(nèi)容,希望對大家的學(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)特效的簡單實(shí)現(xiàn)代碼
- jQuery隨機(jī)密碼生成的方法
- jquery代碼實(shí)現(xiàn)簡單的隨機(jī)圖片瀑布流效果
- jQuery 插件實(shí)現(xiàn)隨機(jī)自由彈跳氣泡樣式
- JS實(shí)現(xiàn)點(diǎn)擊生成UUID的方法完整實(shí)例【基于jQuery】
相關(guān)文章
jquery1.4后 jqDrag 拖動(dòng) 不可用
jquery 升級(jí)到1.4后 jqDrag 拖動(dòng) 不可用的修改方法2010-02-02
jQuery效果 slideToggle() 方法(在隱藏和顯示之間切換)
slideToggle() 方法通過使用滑動(dòng)效果(高度變化)來切換元素的可見狀態(tài)。2011-06-06
jquery拖動(dòng)層效果插件用法實(shí)例分析(附demo源碼)
這篇文章主要介紹了jquery拖動(dòng)層效果插件用法,結(jié)合實(shí)例形式分析了jquery-ui-1.7.2.custom.min.js插件的具體使用技巧,并附帶完整demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04
jQuery實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)層完整代碼
使用jQuery實(shí)現(xiàn)可拖動(dòng)的浮動(dòng)層的完整代碼,復(fù)制保存到html文件就可以體驗(yàn)效果.光標(biāo)移動(dòng)到層上,按住鼠標(biāo)就可以拖動(dòng)層2013-05-05
jQuery leonaScroll 1.1 自定義滾動(dòng)條插件(推薦)
這篇文章主要介紹了jQuery leonaScroll 1.1 自定義滾動(dòng)條插件(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
jQuery實(shí)現(xiàn)文本框郵箱輸入自動(dòng)補(bǔ)全效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)文本框郵箱輸入自動(dòng)補(bǔ)全效果,大家無需輸入完全部郵箱信息,感興趣的小伙伴們可以參考一下2015-11-11
PageSwitch插件實(shí)現(xiàn)100種不同圖片切換效果
這篇文章主要介紹了PageSwitch插件實(shí)現(xiàn)100種不同圖片切換效果,需要的朋友可以參考下2015-07-07
JQuery省市聯(lián)動(dòng)效果實(shí)現(xiàn)過程詳解
這篇文章主要介紹了JQuery省市聯(lián)動(dòng)效果實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05

