2010-09-14 56 views
8

我试图让jQuery来隐藏这个div由于某种原因它不工作我在做什么错jQuery的翻转电不工作

http://stat-me.com/jq.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<style type="text/css"> 
#one{ 
    border:3px solid #00F; 
    width:50%; 
} 
#hideme{ 
    border:3px solid #00F; 
    width:50%; 
    display:none; 
} 
</style> 

<script type="text/javascript" src="../_root/js/jquery/jquery-1.4.2.js"></script> 

<script language="javascript" type="text/javascript"> 



$("#one").click(function() { 
$("#hideme").toggle(); 
}); 

</script> 

</head> 

<body> 

<div id="one"> 
<a href="#">hello</a> 
</div> 

<div id="hideme"> 
hi 
</div> 


</body> 
</html> 

回答

14

您需要:

  1. 使用document.ready
  2. 选择锚点下方的 #one div,而不是t他的div本身

所以它应该是:

$(document).ready(function() { 
    $("#one a").click(function() { 
    $("#hideme").toggle(); 
    }); 
}); 
+1

这两个问题我遇到过maaaany时间:) – 2010-09-15 08:51:16

0

我在使用AJAX具有相同情况和应用本方案。 撰写javascript:void(0);而不是href值中的'#'。这可以防止你在url中添加'#'。 在ajax模式下使用时使用.live()。在.toggle()中,将参数传递为'Drop','slide'等效果,更多地在http://jqueryui.com/toggle/

$(document).ready(function(){ 
     $('#one a').live('click', function(){ 
      $('#hideme').toggle('Drop'); 
      return false; 
     }); 
    }); 

Applying return false;最后阻止我重新加载页面。

只为知识而回答。

0

你不换你的JavaScript在$(document).ready(function(){})等做jQuery是试图找到尚不存在的元素!