[原创 js] 点击展开关闭效果
2008-04-25 13:17
375 查看
因为工作中老是需要这个功能,本来也不是很麻烦,就把它写成一个可重用的函数好了。代码很简单。
//===========================点击展开关闭效果====================================
function openShutManager(oSourceObj,oTargetObj,shutAble,oOpenTip,oShutTip){
var sourceObj = typeof oSourceObj == "string" ? document.getElementById(oSourceObj) : oSourceObj;
var targetObj = typeof oTargetObj == "string" ? document.getElementById(oTargetObj) : oTargetObj;
var openTip = oOpenTip || "";
var shutTip = oShutTip || "";
if(targetObj.style.display!="none"){
if(shutAble) return;
targetObj.style.display="none";
if(openTip && shutTip){
sourceObj.innerHTML = shutTip;
}
} else {
targetObj.style.display="block";
if(openTip && shutTip){
sourceObj.innerHTML = openTip;
}
}
}
函数一共有5个参数,作用如下:
oSourceObj:点击事件的对象,类型为DOM对象,或者对象的id;
oTargetObj:展开关闭的对象,类型为DOM对象,或者对象的id;
shutAble:对象是否可关闭,类型为布尔值,可选,默认为false;
oOpenTip:对象关闭时,点击源的文字,类型为String,可选;
oShutTip:对象打开时,点击源的文字,类型为String,可选.
================================= 下面放demo ==================================
<!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" />
<style type="text/css">
#box,#box2,#box3,#box4{padding:10px;border:1px solid green;}
</style>
<script type="text/javascript">
//===========================点击展开关闭效果====================================
function openShutManager(oSourceObj,oTargetObj,shutAble,oOpenTip,oShutTip){
var sourceObj = typeof oSourceObj == "string" ? document.getElementById(oSourceObj) : oSourceObj;
var targetObj = typeof oTargetObj == "string" ? document.getElementById(oTargetObj) : oTargetObj;
var openTip = oOpenTip || "";
var shutTip = oShutTip || "";
if(targetObj.style.display!="none"){
if(shutAble) return;
targetObj.style.display="none";
if(openTip && shutTip){
sourceObj.innerHTML = shutTip;
}
} else {
targetObj.style.display="block";
if(openTip && shutTip){
sourceObj.innerHTML = openTip;
}
}
}
</script>
<title>无标题文档</title>
</head>
<body>
<p><a href="###" onclick="openShutManager(this,'box')">点击展开</a></p>
<p id="box" style="display:none">
这里面放的是box的内容.
</p>
<p><a href="###" onclick="openShutManager(this,'box2',true)">点击展开</a></p>
<p id="box2" style="display:none">
这里面放的是box的内容.
</p>
<p><a href="###" onclick="openShutManager(this,'box3',false,'点击关闭','点击展开')">点击展开</a></p>
<p id="box3" style="display:none">
这里面放的是box的内容.
</p>
<p><button onclick="openShutManager(this,'box4',false,'点击关闭','点击展开')">点击展开</button></p>
<p id="box4" style="display:none">
这里面放的是box的内容.
</p>
</body>
</html>
//===========================点击展开关闭效果====================================
function openShutManager(oSourceObj,oTargetObj,shutAble,oOpenTip,oShutTip){
var sourceObj = typeof oSourceObj == "string" ? document.getElementById(oSourceObj) : oSourceObj;
var targetObj = typeof oTargetObj == "string" ? document.getElementById(oTargetObj) : oTargetObj;
var openTip = oOpenTip || "";
var shutTip = oShutTip || "";
if(targetObj.style.display!="none"){
if(shutAble) return;
targetObj.style.display="none";
if(openTip && shutTip){
sourceObj.innerHTML = shutTip;
}
} else {
targetObj.style.display="block";
if(openTip && shutTip){
sourceObj.innerHTML = openTip;
}
}
}
函数一共有5个参数,作用如下:
oSourceObj:点击事件的对象,类型为DOM对象,或者对象的id;
oTargetObj:展开关闭的对象,类型为DOM对象,或者对象的id;
shutAble:对象是否可关闭,类型为布尔值,可选,默认为false;
oOpenTip:对象关闭时,点击源的文字,类型为String,可选;
oShutTip:对象打开时,点击源的文字,类型为String,可选.
================================= 下面放demo ==================================
<!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" />
<style type="text/css">
#box,#box2,#box3,#box4{padding:10px;border:1px solid green;}
</style>
<script type="text/javascript">
//===========================点击展开关闭效果====================================
function openShutManager(oSourceObj,oTargetObj,shutAble,oOpenTip,oShutTip){
var sourceObj = typeof oSourceObj == "string" ? document.getElementById(oSourceObj) : oSourceObj;
var targetObj = typeof oTargetObj == "string" ? document.getElementById(oTargetObj) : oTargetObj;
var openTip = oOpenTip || "";
var shutTip = oShutTip || "";
if(targetObj.style.display!="none"){
if(shutAble) return;
targetObj.style.display="none";
if(openTip && shutTip){
sourceObj.innerHTML = shutTip;
}
} else {
targetObj.style.display="block";
if(openTip && shutTip){
sourceObj.innerHTML = openTip;
}
}
}
</script>
<title>无标题文档</title>
</head>
<body>
<p><a href="###" onclick="openShutManager(this,'box')">点击展开</a></p>
<p id="box" style="display:none">
这里面放的是box的内容.
</p>
<p><a href="###" onclick="openShutManager(this,'box2',true)">点击展开</a></p>
<p id="box2" style="display:none">
这里面放的是box的内容.
</p>
<p><a href="###" onclick="openShutManager(this,'box3',false,'点击关闭','点击展开')">点击展开</a></p>
<p id="box3" style="display:none">
这里面放的是box的内容.
</p>
<p><button onclick="openShutManager(this,'box4',false,'点击关闭','点击展开')">点击展开</button></p>
<p id="box4" style="display:none">
这里面放的是box的内容.
</p>
</body>
</html>
相关文章推荐
- [原创 js] 点击展开关闭效果
- JS实现点击图片在当前页面放大并可关闭的漂亮效果
- Js可点击展开关闭的广告代码
- 简单的点击展开/关闭效果
- 通过ValueAnimator实现点击展开和关闭的效果
- JS实现点击图片在当前页面放大并可关闭的漂亮效果
- 简易的点击展开/关闭效果(原生JS版和JQ版)
- js实现展开关闭效果
- 简易的点击展开/关闭效果(原生JS版和JQ版)
- js右下角弹出窗口,点击可关闭效果
- 如何实现js展开收起(折叠)效果呢?例如:一段内容过长了,我限制他显示为20个字内,但是超出了20的就先用省略号代替,然后后面添加一个展开,点击展开后就显示所有的内容,这时候展开按钮就变成了 折叠按钮
- js实现点击向下展开的下拉菜单效果代码
- vue.js 实现点击展开收起动画效果
- js右下角弹出窗口,点击可关闭效果
- JS实现可点击展开与关闭的左侧广告代码
- JS实现可点击展开与关闭的左侧广告代码
- js单个点击展开,再点击折叠效果(IE/FF下通过)
- 原创JS图片放大效果(二) — 多图点击切换,鼠标移上放大 代码很美,步履不停
- js 点击展开和收缩效果
- 原创JS图片放大效果(二) — 多图点击切换,鼠标移上放大