Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@liuxiaomingskm
Last active February 2, 2020 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuxiaomingskm/03fcbb23140e0b408104b9466a7c2328 to your computer and use it in GitHub Desktop.
Save liuxiaomingskm/03fcbb23140e0b408104b9466a7c2328 to your computer and use it in GitHub Desktop.
用JQuery调用随机猫咪图片api, 发送get请求 并更改img中src属性
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<div class="container">
<h1>Welcome To Random Cat Pictures</h1>
<img id="photo" src="https:\/\/dog.ceo\/api\/img\/deerhound-scottish\/n02092002_6780.jpg" alt="">
<button id="btn">Get Random Cat!</button>
</div>
$("#btn").click(function(){
$.get("https://aws.random.cat/meow")
.done(function(data){
$("#photo").attr("src",data.file);
})
.fail(function(){
console.log("Problem occured!");
})
});
img {
height: 200px;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
}
button {
margin: 3px;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment