为什么无法用,在线等!!1
<script type="text/javaScript">
${".ccc"}.click(function(){
alert("aaa");
$(this).addClass("highlight").children("a").show().end().siblings().removeClass("highlight").children("a").hide();
});
</script>
<style type="text/css">
#menu{width:300px;}
.ccc{background: #555;color:#fff;cursor:pointer;}
.highlight{color : #fff;background : green;}
div{padding:0; margin:10px 0;}
div a{background: #888;display:none; float:left;width:300px;}
</style>
</head><body>
<div id="menu">
<div class="ccc">
<span>第一章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
<div class="ccc">
<span>第二章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
<div class="ccc">
<span>第三章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
</div>
</body>
<script type="text/javaScript">
${".ccc"}.click(function(){
alert("aaa");
$(this).addClass("highlight").children("a").show().end().siblings().removeClass("highlight").children("a").hide();
});
</script>
<style type="text/css">
#menu{width:300px;}
.ccc{background: #555;color:#fff;cursor:pointer;}
.highlight{color : #fff;background : green;}
div{padding:0; margin:10px 0;}
div a{background: #888;display:none; float:left;width:300px;}
</style>
</head><body>
<div id="menu">
<div class="ccc">
<span>第一章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
<div class="ccc">
<span>第二章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
<div class="ccc">
<span>第三章</span>
<a>你好吗</a>
<a>你好吗</a>
<a>你好吗</a>
</div>
</div>
</body>
window.onload=function(){
$(".ccc").each(function(){
$(this).click(function(){
$(this).addClass("highlight").children("a").show().end().siblings().removeClass("highlight").children("a").hide();
});
});
}
</script>
<script type="text/javaScript">
window.onload=function(){
$(".ccc").each(function(){
$(this).click(function(){
$(this).addClass("highlight").children("a").show().end().siblings().removeClass("highlight").children("a").hide();
});
});
}
</script>
<style type="text/css">
#menu{width:300px;}
.ccc{background: #555;color:#fff;cursor:pointer;}
.highlight{color : #fff;background : green;}
div{padding:0; margin:10px 0;}
div a{background: #888;display:none; float:left;width:300px;}
</style>
</head> <body>
<div id="menu">
<div class="ccc">
<span>第一章 </span>
<a>你好吗 </a>
<a>你好吗 </a>
<a>你好吗 </a>
</div>
<div class="ccc">
<span>第二章 </span>
<a>你好吗 </a>
<a>你好吗 </a>
<a>你好吗 </a>
</div>
<div class="ccc">
<span>第三章 </span>
<a>你好吗 </a>
<a>你好吗 </a>
<a>你好吗 </a>
</div>
</div>
</body>
$(".ccc").click(function(){
alert("aaa");
$(this).addClass("highlight").children("a").show().end().siblings().removeClass("highlight").children("a").hide();
});
});
就是页面加载完后加载绑定函数,否则没有绑定事件
--
你写的js代码在页面还没加载完之前定义,那代码肯定就找不到 class="ccc"的元素了。
因此,你可以把你的js代码块写到 页面后面的</body>之前.
也可以将js代码块放到
$(function(){
....这里放你的代码
});
--
另外,如果你一定要将它放在前面,而又不放在$(function(){})中的话,
可以使用$('.ccc').live('click',function(){});