+ -
当前位置:首页 → 问答吧 → 新手求助:我的代码哪有错误?

新手求助:我的代码哪有错误?

时间:2010-06-06

来源:互联网

锋利JQUERY  1.3.3jquery代码风格第一个例子,  无法实现。 jquery库为最新非压缩版。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1-5-1 jquery 测试页</title>
<style type="text/css">
#menu {width:300px;}
.has_children {background:#555; color:#fff; cursor:pointer;}
.highlight {color:#fff;background:green;}
*{padding:0; margin:10px 0;}
a{background:#888; display:none; float:left; width:300px}
</style>
<script language="javascript" src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
 $(".has_children").click(function(){
    $(this).addClass("highlight").children("a").show().end().siblings().
removeClass("highlight").children("a").hide();
});
</script>
</head>
<body>
<div id="menu">
 <div class="has_children">
  <span>第1章-认识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>第2章-jquery选择器</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>第3章-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>

作者: qiubi8801   发布时间: 2010-06-06

琢磨了半天  终于找到答案。

解决方法有两种:
1 将行为script在最后插入(即最后一个</div>与</body>之间)
2 仍将行为script插入在<head></head>之间, 在行为script外面嵌套 $(document).ready(function(){
};

作者: qiubi8801   发布时间: 2010-06-06

解决方案1:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1-5-1 jquery 测试页</title>
<style type="text/css">
#menu {width:300px;}
.has_children {background:#555; color:#fff; cursor:pointer;}
.highlight {color:#fff;background:green;}
*{padding:0; margin:10px 0;}
a{background:#888; display:none; float:left; width:300px}
</style>
<!--引入jQuery-->
<script language="javascript" src="js/jquery.js" type="text/javascript"></script>
</head>
<body>
<div id="menu">
    <div class="has_children">
        <span>第1章-认识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>第2章-jquery选择器</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>第3章-jquery中的DOM操作</span>
        <a>3.1-DOM操作的分类</a>
        <a>3.2-jquery中的DOM操作</a>
        <a>3.3-案例研究--某网站超链接和图片提示效果</a>
        <a>3.4-小结</a>
    </div>
</div>
<script type="text/javascript">
    $(".has_children").click(function(){
           $(this).addClass("highlight")
            .children("a").show().end()
        .siblings().removeClass("highlight")
            .children("a").hide();
    });
</script>
</body>
</html>

作者: qiubi8801   发布时间: 2010-06-06

解决方案2

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>1-5-1 jquery 测试页</title>
<style type="text/css">
#menu {width:300px;}
.has_children {background:#555; color:#fff; cursor:pointer;}
.highlight {color:#fff;background:green;}
*{padding:0; margin:10px 0;}
a{background:#888; display:none; float:left; width:300px}
</style>
<!--引入jQuery-->
<script language="javascript" src="js/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $(".has_children").click(function(){
           $(this).addClass("highlight")
            .children("a").show().end()
        .siblings().removeClass("highlight")
            .children("a").hide();
        });
    });
</script>
</head>
<body>
<div id="menu">
    <div class="has_children">
        <span>第1章-认识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>第2章-jquery选择器</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>第3章-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>

作者: qiubi8801   发布时间: 2010-06-06