如何在jQuery中使用.css函数

the*_*ost 0 html javascript css jquery

我使用jquery有点新,下面应该显示一个按钮,当有人点击按钮时,该按钮的字体大小应该改变.

不幸的是,由于我不确定的原因,该按钮不会改变其字体大小.

编辑:此示例与之前的调整相同,它可以工作,但只有鼠标离开按钮区域或双击按钮时才会更改字体.

只需单击一次即可更改字体.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
  $(".button0").click(function() {
    $('.button0').css('font-size', '43px');
  });
});
</script>
<style type="text/css">
.button0{
position:fixed;
left:48px;
top:55px;
font-family:Arial;
font-size:8px;
font-weight:normal;
}
</style>
<body>
<div name="button0"><input class="button0" type="button" style="width: 148px;height: 72px;" value="Button"/></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Dan*_*ite 7

.click调用时,DOM还没有完全加载.您应该将其包装在DOM就绪函数中.

$(function() {
  $(".button0").click(function() {
    $('.button0').css('font-size', '43px');
  });
});
Run Code Online (Sandbox Code Playgroud)

还注意到它不是div.