Darling_lxy 2022-04-22 12:37 采纳率: 71.4%
浏览 44
已结题

jquery的点击事件添加了没有效果

<div id="sotr" class="sotr">
            <a data-index='0' style="background: #aa0000;">1</a>
            <a data-index='1' style="background: #005500;">2</a>
            <a data-index='2' style="background: #aa5500;">3</a>
            <a data-index='3' style="background: #59d57f;">4</a>
            <a data-index='4' style="background: #00ffff;">5</a>
            <a data-index='5' style="background: #ff007f;">6</a>
            <a data-index='6' style="background: #ffff7f;">7</a>
            <a data-index='7' style="background: #5500ff;">8</a>
        </div>
        <div id="click" class="click">提交</div>
        <div id="upset" class="upset">打乱</div>

<script>
/////////初始滑动插件
        var foo = document.getElementById("#sotr");
        Sortable.create(foo, {
            group: "adva"
        });


        function shffle() {
            let array = $('#sotr a')
            let usedIndex = []
            let newArray = []
            let validCount = []
            
            top:
                while (validCount < array.length) {
                    let index = Math.floor(Math.random() * array.length)
                    if (usedIndex.indexOf(index) > -1) {
                        continue top
                    } else {
                        usedIndex.push(index)
                    }
                    validCount++
                    newArray.push(array[index])
                }
                let newarr = shuffle()
            for (let item of newarr) {
                $('#sotr').append(item)
            }
        }
        $('#upset').on('upset', shffle())
        
    </script>

只能刷新打乱,按键不能点击

img

img

  • 写回答

4条回答 默认 最新

  • iMingzhen 2022-04-22 14:54
    关注

    需要做出两步修改

    1. script内容需要在window.onload内执行,以确保dom加载完成
    2. 监听点击的执行函数的写法是错误的

    修改后的完整代码

    <script>
            window.onload = function () {
                /////////初始滑动插件
                var foo = document.getElementById("#sotr");
                Sortable.create(foo, {
                    group: "adva"
                });
    
    
                function shffle() {
                    let array = $('#sotr a')
                    let usedIndex = []
                    let newArray = []
                    let validCount = []
    
                    top:
                    while (validCount < array.length) {
                        let index = Math.floor(Math.random() * array.length)
                        if (usedIndex.indexOf(index) > -1) {
                            continue top
                        } else {
                            usedIndex.push(index)
                        }
                        validCount++
                        newArray.push(array[index])
                    }
                    let newarr = shuffle()
                    for (let item of newarr) {
                        $('#sotr').append(item)
                    }
                }
                $('#upset').on('click', shffle) // 正确写法
            }
    
        </script>
    

    如有帮助请采纳回答

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月30日
  • 已采纳回答 4月22日
  • 创建了问题 4月22日

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法