为什么div变成this就不能实现,如下注释部分
来源:2-4 编程练习
goufully
2019-05-16 15:08:22
<!--2-4-->
<!--鼠标按下和抬起时改变元素的背景颜色和内容-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>习题</title>
<style type="text/css">
div {
width: 200px;
height: 200px;
border: 1px solid red;
text-align: center;
line-height: 200px;
}
</style>
</head>
<body>
<div>背景颜色为白色</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
<script>
$("div").mousedown(function(){
$("div").css("background","red").text("背景颜色为红色");
})
$("div").mouseup(function(){
$("div").css("background","yellow").text("背景颜色为黄色");
})
// $("div").mousedown(function(){
// $("this").css("background","red").text("背景颜色为红色");
// })
// $("div").mouseup(function(){
// $("this").css("background","yellow").text("背景颜色为黄色");
// })
</script>
</body>
</html>
1回答
同学你好, 因为使用双引号包裹this,变成了字符串。 this指向当前元素,应该是变量, 所以没有实现效果。

同学可以下去再测一下哦~~ 另, 代码效果实现的不错
如果帮助到了你, 欢迎采纳!
祝学习愉快~~
相似问题
回答 3
回答 2