【发布时间】:2015-04-22 19:09:59
【问题描述】:
我对编程很陌生,但我似乎无法使用 jquery 添加.hover 效果。
$(document).ready(function() {
$('.mainr').hover(
function() {
$(this).addClass('.mainh')
},
function() {
$(this).removeClass('mainh')
});
});
.main {
position: absolute;
left: 20%;
width: 60%;
height: 500px;
background-color: #D8D8D8;
color: #ffffff;
}
.mainr {
position: absolute;
left: 0px;
top: 50%;
width: 23%;
height: 50%;
border-radius: 50%;
background-color: #0B0B3B;
float: left;
color: #D8D8D8;
text-align: center;
vertical-align: 50%;
font: 20px"Interstate", "Lucida Grande", Arial;
}
.mainh {
position: absolute;
left: 0%;
top: 50%;
width: 23%;
height: 50%;
border-radius: 50%;
background-color: #D8D8D8;
float: left;
color: #0B0B3B;
text-align: center;
vertical-align: 50%;
font: 20px"Interstate", "Lucida Grande", Arial;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="main">
<div class="mainr">Register</div>
</div>
你能告诉我如何将文本在 div 中居中(设置高度)
谢谢,
【问题讨论】:
-
使用 CSS
:hover有什么问题? -
您不应在
addClass调用中添加点。请将$(this).addClass('.mainh')替换为$(this).addClass('mainh')并添加jQuery库 -
也许你可以使用 toggleClass('mainh') 而不是同时使用 addClass 和 removeClass