一个随机句子引用(jQuery与Ajax的引用) 发表于 2017-04-27 | 分类于 demo | 运行效果html代码12345678910<div class="contains"> <div class="quote">“ <span id="quoteText"> </span>” </div> <div class="author"> by <span id="quoteAuthor"> </span></div> <div class="button"> <button class="quoteButton">New quote</button> </div> </div> css代码12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849@import url(https://fonts.googleapis.com/css?family=Lobster);body { background-color: #333; color: #333; font-family: 'Lobster', sans-serif; font-size: 30px;}.contains { width: 600px; padding: 30px 40px; margin: 8% auto auto auto; display:table; background-color:#fff; border-radius: 5px;}.quote { text-align: center; width: 100%; height: auto; clear: both; font-size: 2em; font-weight: 200px;}.author { text-align: right; width: 100%; height: auto; clear: both; font-size: 1em; padding-top:20px;}.button { text-align: center; width: 100%;}.button .quoteButton { height: 45px; border: none; border-radius: 5px; color: #fff; background-color: #333; font-size: 25px; padding: 8px 18px 6px 18px; margin-top:30px; opacity:1;}.button:hover { opacity:0.8;} js代码1234567891011121314151617181920212223242526272829303132333435var colors = ['#16a085', '#27ae60', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6', '#FB6964', '#342224', "#472E32", "#BDBB99", "#77B1A9", "#73A857"];function getQuote() { $.ajax({ headers: { "X-Mashape-Key": "OivH71yd3tmshl9YKzFH7BTzBVRQp1RaKLajsnafgL2aPsfP9V", Accept: "application/json", "Content-Type": "application/x-www-form-urlencoded" }, url: 'https://andruxnet-random-famous-quotes.p.mashape.com/cat=', error: function(XmlHttpRequest, textStatus, errorThrown) { alert("操作失败!"); }, success: function(response) { var r = JSON.parse(response); $('#quoteText').html(r.quote); $('#quoteAuthor').html(r.author); var color = Math.floor(Math.random() * colors.length); $("body").animate({ backgroundColor: colors[color], color: colors[color] },1000); $(".quoteButton").animate({ backgroundColor: colors[color] },1000); } })}$(function() { getQuote(); $(".quoteButton").click(function() { getQuote(); })}) 运行链接:http://codepen.io/wzc/full/XNeoJg/