微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将鼠标悬停在框上,它应该改变其位置随机删除 jquery 而不影响代码

如何解决将鼠标悬停在框上,它应该改变其位置随机删除 jquery 而不影响代码

我已经做到了这一点,但代码不起作用。它在小提琴中工作,但在这里不是。我附上了一个我想要的解决方案和我所做的代码。我以某种方式发现使用 jQuery 它可以工作,但我不想使用 jQuery 或告诉我如何在 sublime 文本中使用 jQuery。

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <Meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Catch Me</title>
<style>
button {
    background-color: rgb(228,6,248);
    border: none;
    color: white;
    padding: 20px 20px;
    text-align: center;
    position: relative;
    top: 100px;
    left: 100px;
}
</style>
</head>
<body>
    <div class="main">
    <button name="button">Catch Me</button>
    </div>
    
</body>
</html>
<script type="text/javascript">
$(function(){
    $("button").on({
        mouSEOver:function(){
            $(this).css({
                left:(Math.random()*200)+"px",top:(Math.random()*200)+"px",});
        }
    });
});
</script>
我想要的解决方案是这样的: https://ninjasfiles.s3.amazonaws.com/0000000000001878.gif

解决方法

请试试这个:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>Catch Me</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style>
button {
    background-color: rgb(228,6,248);
    border: none;
    color: white;
    padding: 20px 20px;
    text-align: center;
    position: relative;
    top: 100px;
    left: 100px;
}
</style>
</head>
<body>
    <div class="main">
    <button name="button">Catch Me</button>
    </div>


<script type="text/javascript">
    $(document).ready(function(){
        $("button").mouseover(function() {
            $(this).css("left","" + (Math.random()*200) + "px");
            $(this).css("top","" + (Math.random()*200) + "px");
        });
    });
</script>
    
</body>
</html>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。