<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js">
</script>
<style>
img {
width: 500px;
}
</style>
</head>
<body>
<button>获取头像</button>
<img src="" alt="">
<script>
var head = '';
function gethead() {
axios({
method: 'get',
url: 'https://api.muxiaoguo.cn/api/sjtx?method=pc',
params: {}
}).then((res) => {
// console.log(res.data.data.imgurl);
head = res.data.data.imgurl;
console.log(head);
})
return head;
}
$(function () {
$('button').click(function () {
gethead()
$('img').attr('src', head)
// console.log(head);
})
})
</script>
</body>
</html>
用木小果api做的随机获取头像 注释是写的时候打的草稿可以忽略
如果点击获取没出来 接口网不太好 等一分钟再点一次就好了