Vue+jQuery+axios练习小案例【随机获取笑话】

一、效果演示

在这里插入图片描述

二、完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引入vue.js和axios.js和jquery.js -->
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <style>
        input {
            height: 30px;
            line-height: 30px;
        }

        #inputDiv {
            text-align: center;
        }

        #jbtn {
            line-height: 30px;
        }

        p {
            width: 100%;
            padding: 5px;
        }
    </style>
</head>

<body>

    <div id="jokeDiv">
        <div id="inputDiv">
            <input type="text" placeholder="请输入要获取的笑话数量" id="jcount" v-model="jcount">
            <input type="button" value="获取随机笑话" @click="getJokes" id="jbtn">
        </div>
        <br>笑话内容:
        <hr>
        <p v-for="joke in jokes">{{joke}}</p>
    </div>
</body>
<script>
    /*
    get方式发送ajax请求:
         格式1: axios.get(地址?key=value&key2=value2).then(function(response){},function(error){});
         格式2: axios.get(地址,{params:{key1:value1...}}).then(function(response){},function(error){});
    */
    /*
     接口1:随机笑话
         请求地址:https://autumnfish.cn/api/joke/list
         请求方法:get
         请求参数:num(笑话条数,数字)
         响应内容:随机笑话
     */
    new Vue({
        el: "#jokeDiv",
        data: {
            jcount: 1,
            jokes: [],
        },
        methods: {
            getJokes() {
                var t = this; //将对象先记录下来  重要
                //发送ajax异步请求
                axios.get("https://autumnfish.cn/api/joke/list", { params: { "num": this.jcount } }).then(
                    function (response) {
                        t.jokes = response.data.jokes; //修改该vue对象的数据
                        t.$nextTick(()=>{t.updatedColor()}) //当vue更新完DOM元素后,执行回调函数,进行背景色设置
                    }, function (error) {

                    })
            },
            updatedColor() {
                var colors = ['#FFCCCC', '#FFFF99', '#FFCC99', '#CCFF99', '#CCCCCC', '#FFFFCC', '#CCCCFF', '#99CCFF'];
                var $plist = $("p")
                //jquery的对象方法遍历
                $plist.each(function (index, element) {
                    var randomIndex = parseInt(Math.random() * colors.length);
                    console.log(element)
                    $(element).css("background-color", colors[randomIndex])
                })
            }
        },
    })
</script>

</html>

三、相关说明

  • jquery中遍历时不能使用for of语法,因为jquery3才开始支持for of
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迟到_啦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值