问题描述
我已经做到了这一点,但代码不起作用。它在小提琴中工作,但在这里不是。我附上了一个我想要的解决方案和我所做的代码。我以某种方式发现使用 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>
解决方法
请试试这个:
<!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>