HTML 代码如下;
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是继Prototype之后又一个优秀的JavaScript库,它是一个由 John Resig 创建于2006年1月的开源项目。jQuery凭借简洁的语法和跨平台的兼容性,极大地简化了JavaScript开发人员遍历HTML文档、操作DOM、处理事件、执行动画和开发Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的设计思路和编写程序的方式。
</div>
</div>
</body>
CSS代码: .content { padding: 10px; text-indent: 2em; border-top: 1px solid #0050D0;display:block;display:none; }
点击展开、点击关闭
$(function(){
$("#panel h5.head").bind("click",function(){
var $content = $(this).next("div.content");
if($content.is(":visible")){
$content.hide();
}else{
$content.show();
}
})
})
鼠标划过
$(function(){
$("#panel h5.head").bind("mouseover",function(){
$(this).next("div.content").show();
});
$("#panel h5.head").bind("mouseout",function(){
$(this).next("div.content").hide();
})
})
事件合并 鼠标经过展开,鼠标移开关上
$(function(){
$("#panel h5.head").hover(function(){
$(this).next("div.content").show();
},function(){
$(this).next("div.content").hide();
})
})
事件合并 鼠标点击打开,点击关闭
$(function(){
$("#panel h5.head").toggle(function(){
$(this).addClass("highlight");
$(this).next("div.content").show();
},function(){
$(this).removeClass("highlight");
$(this).next("div.content").hide();
});
})
<body>
<div id="panel">
<h5 class="head">什么是jQuery?</h5>
<div class="content">
jQuery是继Prototype之后又一个优秀的JavaScript库,它是一个由 John Resig 创建于2006年1月的开源项目。jQuery凭借简洁的语法和跨平台的兼容性,极大地简化了JavaScript开发人员遍历HTML文档、操作DOM、处理事件、执行动画和开发Ajax。它独特而又优雅的代码风格改变了JavaScript程序员的设计思路和编写程序的方式。
</div>
</div>
</body>
CSS代码: .content { padding: 10px; text-indent: 2em; border-top: 1px solid #0050D0;display:block;display:none; }
点击展开、点击关闭
$(function(){
$("#panel h5.head").bind("click",function(){
var $content = $(this).next("div.content");
if($content.is(":visible")){
$content.hide();
}else{
$content.show();
}
})
})
鼠标划过
$(function(){
$("#panel h5.head").bind("mouseover",function(){
$(this).next("div.content").show();
});
$("#panel h5.head").bind("mouseout",function(){
$(this).next("div.content").hide();
})
})
事件合并 鼠标经过展开,鼠标移开关上
$(function(){
$("#panel h5.head").hover(function(){
$(this).next("div.content").show();
},function(){
$(this).next("div.content").hide();
})
})
事件合并 鼠标点击打开,点击关闭
$(function(){
$("#panel h5.head").toggle(function(){
$(this).addClass("highlight");
$(this).next("div.content").show();
},function(){
$(this).removeClass("highlight");
$(this).next("div.content").hide();
});
})