日期:2014-05-16  浏览次数:20404 次

我在自学Jquery,但是我按照书本上敲的,没错啊,可是就是没有效果,也不会报错,很纠结..请大家帮帮忙!
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script src="../../BOOK/书籍/Jquery/jquery-1.3.2.js"></script>
<script type="text/jscript">
$(".has_children").click(function(){
$(this).addClass("highlight")
.children("a").show().end()
.siblings().removeClass("highlight")
.children("a").hide();
});
</script>
<title>导航栏</title>
<style type="text/css">
#menu{width:300px;}
.has_children{ background:#555; color:#fff; cursor: pointer;}
.highlight{ color:#fff; background: green;}
div{ padding:0; margin:10px 0;}
a{ background:#888; display:none; float:left; width:300px;}
</style>
</head>

<body>

<div id="menu">
<div class="has_children">
<span>第一章、认识JQuery</span>
<a>1.1-JavaScript和JavaScript库</a>
<a>1.2-加入Jquery</a>
<a>1.3-编写简单Jquery代码</a>
<a>1.4-Jquery对象和DOM对象</a>
<a>1.5-解决Jquery和其他库的冲突</a>
<a>1.6-Jquery开发工具和插件呢</a>
<a>1.7-小结</a>
</div>
<div class="has_children">
<span>第二章、认识选择器</span>
<a>2.1-Jquery的选择器是什么</a>
<a>2.2-Jquery的选择器的优势</a>
<a>2.3-Jquery选择器</a>
<a>2.4-应用Jquery改写实例</a>
<a>2.5-选择器的一些注意事项</a>
<a>2.6-案例研发——类似淘宝网品牌的列表效果</a>
<a>2.7-还有其他的选择器吗</a>
<a>2.8-小结</a>
</div>
<div class="has_children">
<span>第三章、Jquery中的DOM操作</span>
<a>3.1-DOM操作分类</a>
<a>3.2-Jquery中的DOM操作</a>
<a>3.3-案例研发——某网站超链接和图片提示效果</a>
<a>3.4-小结</a>
</div>
</div>
</body>
</html>


------解决方案--------------------
jquery那里应该这样写的
<script type="text/jscript">
$(function(){
$(".has_children").click(function(){
$(this).addClass("highlight")
.children("a").show().end()
.siblings().removeClass("highlight")
.children("a").hide();
});
});
</script>

------解决方案--------------------
因为你在绑定时间的时候,这个DOM结构都还没有,相当于是没找到元素。解决办法
1、使用live来绑定,如$(".has_children").live('click',***);
2、跟ucjatteyma说的一样,在domReady的时候注册 $(function(){$(".has_children").click(****); });