JQuery切换方法添加意外显示:无

时间:2017-03-24 06:25:52

标签: jquery

以下html显示3个方块

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="content-language" content="en-us" />
<title></title>
<script type="text/javascript" src="jquery.js">
</script>
<script     type="text/javascript" src="Example.js"></script>
<link rel="stylesheet" href="Example.css" type="text/css" />
</head>
<body>
<div class="tmpPattern">
<div></div>
<div></div>
<div></div>
</div>
</body>
</html>

在Js中,我希望添加代码以在悬停和切换时更改颜色。

var tmpExample = {
 ready:function(){
   $(document).ready(
    function($e) {
      $('div.tmpPattern').hover(
        function($e){
          $(this).find('div').addClass('tmpExampleOver');
        },
        function($e){
          $(this).find('div').removeClass('tmpExampleOver');
        }
        );

      $('div.tmpPattern').toggle(
        function($e){
          $(this).find('div').addClass('tmpExampleOn');
        },
        function($e){
          $(this).find('div').removeClass('tmpExampleOn');
        }
        );
   }
  );
 } 
}

//$(document).ready(tmpExample.ready);
if ($) {
$(document).ready (
 function(){
  tmpExample.ready();
 }
);


}

描述颜色的CSS如下:

body {
font: 16px sans-serif;
}

div.tmpPattern {
height: 100px;
}
div.tmpPattern div {
height: 88px;
width: 88px;
border: 1px solid rgb(200, 200, 200);
float: left;
margin: 0 3px;
background: #cfdbe5;
}
body div.tmpExampleOver {
background: #acd1ed;
}
body div.tmpExampleOn {
background: #7e93a4;
}

如果我评论切换代码,则正确显示正方形,并且悬停功能也有效。如果我取消注释切换代码,则平方消失!使用调试器,我可以看到style="display:none"被添加。我不明白为什么。

0 个答案:

没有答案
相关问题