代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
<script src="jquery.js"></script>
<style>
.menu{ width:20px; height:20px; background:red; position:relative;}
.menu div{border:3px red solid; width: 100px; height:100px; background:black; position:absolute; top:20px;}
.add_class{ border:3px blue solid;}
</style>
</head>
<body>
<div class="menu">
<div></div>
</div>
<script>
$(".menu").click(
function(){
$(".menu div").addClass("add_class")
}
)
</script>
</body>
</html>
楼上都不对。
其实
add_class
已被添加,只是由于class
的优先级过低,新样式被忽略了。正确做法是:https://jsfiddle.net/hsfzxjy/...