div.prototype.show=function(e){
e = e||event
e.cancelBubble=true
if(jilu) return;
这里不应该退出函数吧
jilu为真的时候就应该document.body.removeChild(jilu)还有,lz你的命名习惯很不好
e = e||event
e.cancelBubble=true
if(jilu) return;
这里不应该退出函数吧
jilu为真的时候就应该document.body.removeChild(jilu)还有,lz你的命名习惯很不好
jilu不为空
就是已经有div了
如果不退出 就往下执行 就会又新建一个div出来....命名完全不行噢 有没有命名的资料呀?????????
var jilu; //记录哪个元素被打开了
document.onclick=function(){
if(!jilu)
{document.body.removeChild(jilu);jilu=undefined}} //关闭打开的divfunction div(id){
this.obj=document.getElementById(id);
//this.isopen=false 本来打用个私有量来控制是不是打开了的 但是在document.onclick不知道怎么改变它
}div.prototype.button=function(){
var self =this
var input = document.createElement("input")
input.type="button";
input.value="Btn";
input.onclick=function() { self.show(); }
document.body.appendChild(input)} //这个就是创建一个button
div.prototype.show=function(){ //如果已经有了div 就返回
var div=document.createElement("div");
div.setAttribute("id",this.obj.id+"_Div");
div.className = "ss";
div.style.width="100px";
div.style.height="100px";
//div.style.left=input.offsetLeft;
//div.style.top=input.offsetTop;
div.innerText=this.obj.id;
if(jilu)
{
document.body.removeChild(jilu);
}
jilu=div;
document.body.appendChild(div);}
var oo = new div("s")
oo.button()
var cc = new div("ss")
cc.button()
</script>
-------------------------------
测试通过了,位置怎么排你自己看着办,你的代码写的真是乱!
点击其他空白地方
div并不会消失。
<style type="text/css">
.ss{ width:50px; height:50px; background-color:#FFFF99; position:absolute}
</style>
<body></body>
<script>
function $(obj){return document.getElementById(obj);}
function createButton()
{
var oBtn=document.createElement("input");
oBtn.type="button";
oBtn.style.marginLeft="100px";
oBtn.value="按钮";
//按钮失去焦点时,存在div删除
oBtn.onblur=function(){
if($("show")!=null)
{
document.body.removeChild($("show"));
}
}
oBtn.onclick=function(){
createDiv(oBtn);
}
document.body.appendChild(oBtn);
}
//创建层
function createDiv(obj)
{
//其中一个按钮点击时,要是div存在的话删除,重新创建(也可以改变div的位置)
if($("show")!=null)
{
document.body.removeChild($("show"));
}
var oDiv=document.createElement("div");
oDiv.setAttribute("id","show");
oDiv.style.left=obj.offsetLeft+35+"px";
oDiv.style.top=obj.offsetTop+20+"px";
oDiv.className="ss";
oDiv.style.border="1px solid red";
document.body.appendChild(oDiv);
}
createButton();
createButton();
</script>重新写两个下,lz你写的是有的乱
是document.onclick的问题 ,不能这么写,你要明白执行了button的事件,就会接着执行document.onclick
1楼不是已经说得很明白了么
……
e.cancelBubble=true
if(jilu) document.body.removeChild(jilu); //如果已经有了div 就返回
var div=document.createElement("div")
div.className = "ss"
……
1楼的写法跟我的写法就是一样的
但是我的问题并不是这个
因为jilu是记录打开的那个div 但是没有记录是按的哪个按纽打开 2楼就没有这么判断 他是只要有点击,就删除会去删除一个div 在生成div(PS:这就避免了我的那中判断,但是每次执行的代码就我原来的要多,我不喜欢这样处理......... 且他也没有处理空白处按键,和button上的事件禁止冒泡)