我想实现屏幕遮罩功能。就是那种屏幕变黑,某个层可以看到。
现在的问题是怎么实现点击高亮层的关闭,就执行关闭操作。HTML
function g()
{
var c = new Shade();
c.Show("登陆",1,400,300);
}<div>
<a href="javascript:g();">登陆</a>
</div>JS
var Shade = function()
{
if (document.getElementById("__bg__") == null)
{
var b = document.createElement("div"); //create div element if not exists
var s = b.style;
b.id = "__bg__";
s.position = "absolute";
s.zIndex = 99;
s.top = b.style.left = 0 + "px";
s.display = "none";
s.filter = "Alpha(opacity=50)";
s.opacity = 0.5;
s.width = "100%";
s.height = "100%";
s.backgroundColor = "#111";
b.onclick = function() { Shade.prototype.Hide() };
document.body.appendChild(b);
//Debug check the function has been run.
//alert("ok");
s = b = null;
}
else { } window.onresize = function()
{
Shade.prototype.Resize(); //resize the dom element
//alert("resized");
}};Shade.prototype.Show = function(o, i, w, h)
{
//create mask div
if (document.getElementById("__m__") == null)
{
var m = document.createElement("div");
document.body.appendChild(m);
var t = m.style;
m.id = "__m__";
t.position = "absolute";
t.zIndex = document.getElementById("__bg__").style.zIndex + 1;
t.width = (w || 100) + "px";
t.height = (h || 75) + "px";
t.backgroundColor = "#fff";
t = m = null;
}
document.getElementById("__m__").style.display = "";
document.getElementById("__m__").innerHTML = (function()
{
var title = "<div style='width:100%;height:20px;line-height:20px'>消息<span style='float:right' onclick='javascript:eval(" + this.prototype.Hide() + ");'>关闭</span></div>";
switch (i || 1)
{
case 1:
return title + "<div>" + o + "</div>";
break;
}
} ()); Shade.prototype.Resize();
document.getElementById("__bg__").style.display = "";
//TODO here,somehow would hide the select html element for IE6 bug //Debug,check the function has been run.
//alert("Show Method is running");
//return the instance of object,so can call method Show().Hide() like jquery.
return this;
};Shade.prototype.Hide = function()
{
document.getElementById("__bg__").style.display = "none";
document.getElementById("__m__").style.display = "none";
//Debug,check the function has been run.
//alert("Hide Method is running");
return this;
}Shade.prototype.Resize = function()
{
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
document.getElementById("__bg__").style.width = w+"px";
document.getElementById("__bg__").style.height = h+"px";
var t = (parseInt(h) - parseInt(document.getElementById("__m__").style.height)) / 2 + "px";
var l = (parseInt(w) - parseInt(document.getElementById("__m__").style.width)) / 2 + "px";
document.getElementById("__m__").style.top = t;
document.getElementById("__m__").style.left = l;
//TODO here do sth. for other elements in the page .
w = h = null;
t = l = null;
}问题就在Show方法里,var title 这行代码,怎么样才可以执行c.Hide()或者Shade.prototype.Hide().原谅我的JS基本功不好。
现在的问题是怎么实现点击高亮层的关闭,就执行关闭操作。HTML
function g()
{
var c = new Shade();
c.Show("登陆",1,400,300);
}<div>
<a href="javascript:g();">登陆</a>
</div>JS
var Shade = function()
{
if (document.getElementById("__bg__") == null)
{
var b = document.createElement("div"); //create div element if not exists
var s = b.style;
b.id = "__bg__";
s.position = "absolute";
s.zIndex = 99;
s.top = b.style.left = 0 + "px";
s.display = "none";
s.filter = "Alpha(opacity=50)";
s.opacity = 0.5;
s.width = "100%";
s.height = "100%";
s.backgroundColor = "#111";
b.onclick = function() { Shade.prototype.Hide() };
document.body.appendChild(b);
//Debug check the function has been run.
//alert("ok");
s = b = null;
}
else { } window.onresize = function()
{
Shade.prototype.Resize(); //resize the dom element
//alert("resized");
}};Shade.prototype.Show = function(o, i, w, h)
{
//create mask div
if (document.getElementById("__m__") == null)
{
var m = document.createElement("div");
document.body.appendChild(m);
var t = m.style;
m.id = "__m__";
t.position = "absolute";
t.zIndex = document.getElementById("__bg__").style.zIndex + 1;
t.width = (w || 100) + "px";
t.height = (h || 75) + "px";
t.backgroundColor = "#fff";
t = m = null;
}
document.getElementById("__m__").style.display = "";
document.getElementById("__m__").innerHTML = (function()
{
var title = "<div style='width:100%;height:20px;line-height:20px'>消息<span style='float:right' onclick='javascript:eval(" + this.prototype.Hide() + ");'>关闭</span></div>";
switch (i || 1)
{
case 1:
return title + "<div>" + o + "</div>";
break;
}
} ()); Shade.prototype.Resize();
document.getElementById("__bg__").style.display = "";
//TODO here,somehow would hide the select html element for IE6 bug //Debug,check the function has been run.
//alert("Show Method is running");
//return the instance of object,so can call method Show().Hide() like jquery.
return this;
};Shade.prototype.Hide = function()
{
document.getElementById("__bg__").style.display = "none";
document.getElementById("__m__").style.display = "none";
//Debug,check the function has been run.
//alert("Hide Method is running");
return this;
}Shade.prototype.Resize = function()
{
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
document.getElementById("__bg__").style.width = w+"px";
document.getElementById("__bg__").style.height = h+"px";
var t = (parseInt(h) - parseInt(document.getElementById("__m__").style.height)) / 2 + "px";
var l = (parseInt(w) - parseInt(document.getElementById("__m__").style.width)) / 2 + "px";
document.getElementById("__m__").style.top = t;
document.getElementById("__m__").style.left = l;
//TODO here do sth. for other elements in the page .
w = h = null;
t = l = null;
}问题就在Show方法里,var title 这行代码,怎么样才可以执行c.Hide()或者Shade.prototype.Hide().原谅我的JS基本功不好。
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
<script>
function g()
{
c = new Shade();
c.Show("登陆",1,400,300);
}
var Shade = function()
{
if (document.getElementById("__bg__") == null)
{
var b = document.createElement("div"); //create div element if not exists
var s = b.style;
b.id = "__bg__";
s.position = "absolute";
s.zIndex = 99;
s.top = b.style.left = 0 + "px";
s.display = "none";
s.filter = "Alpha(opacity=50)";
s.opacity = 0.5;
s.width = "100%";
s.height = "100%";
s.backgroundColor = "#111";
b.onclick = function() { Shade.prototype.Hide() };
document.body.appendChild(b);
//Debug check the function has been run.
//alert("ok");
s = b = null;
}
else { } window.onresize = function()
{
Shade.prototype.Resize(); //resize the dom element
//alert("resized");
} }; Shade.prototype.Show = function(o, i, w, h)
{
//create mask div
if (document.getElementById("__m__") == null)
{
var m = document.createElement("div");
document.body.appendChild(m);
var t = m.style;
m.id = "__m__";
t.position = "absolute";
t.zIndex = document.getElementById("__bg__").style.zIndex + 1;
t.width = (w || 100) + "px";
t.height = (h || 75) + "px";
t.backgroundColor = "#fff";
t = m = null;
}
document.getElementById("__m__").style.display = "";
document.getElementById("__m__").innerHTML = (function()
{
var title = " <div style='width:100%;height:20px;line-height:20px'>消息 <span style='float:right' onclick='c.Hide();'>关闭 </span> </div>";
switch (i || 1)
{
case 1:
return title + " <div>" + o + " </div>";
break;
}
} ());
Shade.prototype.Resize();
document.getElementById("__bg__").style.display = "";
//TODO here,somehow would hide the select html element for IE6 bug //Debug,check the function has been run.
//alert("Show Method is running");
//return the instance of object,so can call method Show().Hide() like jquery.
return this;
}; Shade.prototype.Hide = function()
{
document.getElementById("__bg__").style.display = "none";
document.getElementById("__m__").style.display = "none";
//Debug,check the function has been run.
//alert("Hide Method is running");
return this;
} Shade.prototype.Resize = function()
{
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
document.getElementById("__bg__").style.width = w+"px";
document.getElementById("__bg__").style.height = h+"px";
var t = (parseInt(h) - parseInt(document.getElementById("__m__").style.height)) / 2 + "px";
var l = (parseInt(w) - parseInt(document.getElementById("__m__").style.width)) / 2 + "px";
document.getElementById("__m__").style.top = t;
document.getElementById("__m__").style.left = l;
//TODO here do sth. for other elements in the page .
w = h = null;
t = l = null;
}
</script>
</head><body>
<div>
<a href="javascript:g();">登陆 </a>
</div>
</body></html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>提示信息框 </title>
<style type="text/css">
a{ color:#000; font-size:12px;text-decoration:none}
a:hover{ color:#900; text-decoration:underline}
body{background:;filter:progid:DXImageTransform.Microsoft.Gradient(gradientType=0,startColorStr=#ffffff,endColorStr=#003366); overflow:hidden}
#massage_box{ position:absolute; left:expression((body.clientWidth-350)/2); top:expression((body.clientHeight-200)/2); width:350px; height:200px;filter:dropshadow(color=#666666,offx=3,offy=3,positive=2); z-index:2; visibility:hidden}
#mask{ position:absolute; top:0; left:0; width:expression(body.scrollWidth); height:expression(body.scrollHeight); background:#666; filter:ALPHA(opacity=60); z-index:1; visibility:hidden}
.massage{border:#036 solid; border-width:1 1 3 1; width:95%; height:95%; background:#fff; color:#036; font-size:12px; line-height:150%}
.header{background:#036; height:10%; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; padding:3 5 0 5; color:#fff}
</style>
<!--实现层移动-->
<script language="javascript">
var Obj=''
document.onmouseup=MUp
document.onmousemove=MMovefunction MDown(Object){
Obj=Object.id
document.all(Obj).setCapture()
pX=event.x-document.all(Obj).style.pixelLeft;
pY=event.y-document.all(Obj).style.pixelTop;
}function MMove(){
if(Obj!=''){
document.all(Obj).style.left=event.x-pX;
document.all(Obj).style.top=event.y-pY;
}
}function MUp(){
if(Obj!=''){
document.all(Obj).releaseCapture();
Obj='';
}
}
</script>
</head> <body>
<div id="massage_box"> <div class="massage">
<div class="header" onmousedown=MDown(massage_box)> <div style="display:inline; width:150px; position:absolute">本站提示信息 </div>
<span onClick="massage_box.style.visibility='hidden'; mask.style.visibility='hidden'" style="float:right; display:inline; cursor:hand">× </span> </div>
<ul style="margin-right:25"> <li>你的要放的代码
</li> <input type="text" /> <br /> <input type="button" value="这是按扭" /> <a href="http://www.baidu.com">百度</a>
<li> </li> <li> </li> </ul> </div> </div>
<div id="mask"> </div>
<span onClick="mask.style.visibility='visible';massage_box.style.visibility='visible'" style="cursor:hand"> <a href="#">显示提示信息 </a> </span>
<br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br>bottom here
</body>
</html>
var d=new Shade();那么就没有用了。所以我觉得简单的用c.Hide()来代替治标不治本。还有就是我比较奇怪,你把所有的JS都放一个文件里是没有问题的,但是我是把JS拿出来写的。貌似在我代码里按照你的写法是不通过的。谢谢。
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>test</title>
<script>
function g()
{
var c = new Shade();
c.Show("登陆",1,400,300);
}
var Shade = function()
{
if (document.getElementById("__bg__") == null)
{
var b = document.createElement("div"); //create div element if not exists
var s = b.style;
b.id = "__bg__";
s.position = "absolute";
s.zIndex = 99;
s.top = b.style.left = 0 + "px";
s.display = "none";
s.filter = "Alpha(opacity=50)";
s.opacity = 0.5;
s.width = "100%";
s.height = "100%";
s.backgroundColor = "#111";
b.onclick = function() { Shade.prototype.Hide() };
document.body.appendChild(b);
//Debug check the function has been run.
//alert("ok");
s = b = null;
}
else { } window.onresize = function()
{
Shade.prototype.Resize(); //resize the dom element
//alert("resized");
} }; Shade.prototype.Show = function(o, i, w, h)
{
//create mask div
if (document.getElementById("__m__") == null)
{
var m = document.createElement("div");
document.body.appendChild(m);
var t = m.style;
m.id = "__m__";
t.position = "absolute";
t.zIndex = document.getElementById("__bg__").style.zIndex + 1;
t.width = (w || 100) + "px";
t.height = (h || 75) + "px";
t.backgroundColor = "#fff";
t = m = null;
}
document.getElementById("__m__").style.display = "";
document.getElementById("__m__").innerHTML = (function()
{
var title = " <div style='width:100%;height:20px;line-height:20px'>消息 <span style='float:right' onclick='new Shade().Hide();'>关闭 </span> </div>";
switch (i || 1)
{
case 1:
return title + " <div>" + o + " </div>";
break;
}
} ());
Shade.prototype.Resize();
document.getElementById("__bg__").style.display = "";
//TODO here,somehow would hide the select html element for IE6 bug //Debug,check the function has been run.
//alert("Show Method is running");
//return the instance of object,so can call method Show().Hide() like jquery.
return this;
}; Shade.prototype.Hide = function()
{
document.getElementById("__bg__").style.display = "none";
document.getElementById("__m__").style.display = "none";
//Debug,check the function has been run.
//alert("Hide Method is running");
return this;
} Shade.prototype.Resize = function()
{
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
document.getElementById("__bg__").style.width = w+"px";
document.getElementById("__bg__").style.height = h+"px";
var t = (parseInt(h) - parseInt(document.getElementById("__m__").style.height)) / 2 + "px";
var l = (parseInt(w) - parseInt(document.getElementById("__m__").style.width)) / 2 + "px";
document.getElementById("__m__").style.top = t;
document.getElementById("__m__").style.left = l;
//TODO here do sth. for other elements in the page .
w = h = null;
t = l = null;
}
</script>
</head><body>
<div>
<a href="javascript:g();">登陆 </a>
</div>
</body></html>
<!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" />
<title>无标题文档</title>
<script type="text/javascript">
var Shade = function()
{
if (document.getElementById("__bg__") == null)
{
var b = document.createElement("div"); //create div element if not exists
var s = b.style;
b.id = "__bg__";
s.position = "absolute";
s.zIndex = 99;
s.top = b.style.left = 0 + "px";
s.display = "none";
s.filter = "Alpha(opacity=50)";
s.opacity = 0.5;
s.width = "100%";
s.height = "100%";
s.backgroundColor = "#111";
var self = this;
b.onclick = function() { self.Hide() };
document.body.appendChild(b);
//Debug check the function has been run.
//alert("ok");
s = b = null;
}
else { } window.onresize = function()
{
Shade.prototype.Resize(); //resize the dom element
//alert("resized");
} }; Shade.prototype.Show = function(o, i, w, h)
{
//create mask div
if (document.getElementById("__m__") == null)
{
var m = document.createElement("div");
document.body.appendChild(m);
var t = m.style;
m.id = "__m__";
t.position = "absolute";
t.zIndex = document.getElementById("__bg__").style.zIndex + 1;
t.width = (w || 100) + "px";
t.height = (h || 75) + "px";
t.backgroundColor = "#fff";
t = m = null;
}
var self = this;
document.getElementById("__m__").style.display = "";
document.getElementById("__m__").innerHTML = (function()
{
var title = " <div style='width:100%;height:20px;line-height:20px'>消息 <span style='float:right' >关闭 </span> </div>";
switch (i || 1)
{
case 1:
return title + " <div>" + o + " </div>";
break;
}
} ());
document.getElementById("__m__").getElementsByTagName("span")[0].onclick = function(){
self.Hide();
}; //Shade.prototype.Resize();
this.Resize();
document.getElementById("__bg__").style.display = "";
//TODO here,somehow would hide the select html element for IE6 bug //Debug,check the function has been run.
//alert("Show Method is running");
//return the instance of object,so can call method Show().Hide() like jquery.
return this;
}; Shade.prototype.Hide = function()
{
document.getElementById("__bg__").style.display = "none";
document.getElementById("__m__").style.display = "none";
//Debug,check the function has been run.
//alert("Hide Method is running");
return this;
} Shade.prototype.Resize = function()
{
var w = Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth);
var h = Math.max(document.documentElement.scrollHeight, document.documentElement.clientHeight);
document.getElementById("__bg__").style.width = w+"px";
document.getElementById("__bg__").style.height = h+"px";
var t = (parseInt(h) - parseInt(document.getElementById("__m__").style.height)) / 2 + "px";
var l = (parseInt(w) - parseInt(document.getElementById("__m__").style.width)) / 2 + "px";
document.getElementById("__m__").style.top = t;
document.getElementById("__m__").style.left = l;
//TODO here do sth. for other elements in the page .
w = h = null;
t = l = null;
}
//问题就在Show方法里,var title 这行代码,怎么样才可以执行c.Hide()或者Shadefunction g()
{
var c = new Shade();
c.Show("登陆",1,400,300);
}
</script>
</head><body>
<div>
<a href="javascript:g();">登陆 </a>
</div>
</body>
</html>
另外,我刚对1楼说过我页面和代码分开有问题,事实上我没有注意到您使用了全局变量,您用的c=new Shade(),而我代码用的是var c=new Shade(),这样我代码中的c就是局部变量了。这点我学习到了。谢谢。6楼中有提到闭包,我开始也曾想到这点,可是学的不精,并没有深究。我在您的代码上去掉了self,因为在闭包外面,this还是指向的原来的变量。我加上了onclick = function() { Shade.prototype.Hide()};我尝试了使用this.Hide()无法运行,换成Shade.prototype.Hide()就可以。我想恐怕是我又不掌握哪个知识点,还望各位大大指教
var self=this;
document.getElementById("__m__").innerHTML =....;
document.getElementById("__m__").getElementsByTagName("span")[0].onclick = function(){
self.Hide();
};
这样就可以。但是直接写this.Hide();是不行的。
囧
var self=this
相当于是
Shade.prototype在调用,Shade.prototype里面有Hide()吗?有,所以ok.而你在
document.getElementById("__m__").getElementsByTagName("span")[0].onclick 里面使用的时候是
document.getElementById("__m__").getElementsByTagName("span")[0]在调用,
document.getElementById("__m__").getElementsByTagName("span")[0]里面有Hide()吗?没有,所以不行.this永远是指向调用这个函数的对象。