<div id="wrapper">
<button id="ppp">button</button>
<div id="content" style="display:none;"></div>
</div>
,如果我按一下按钮,内容会显示(开),关于javascript showbox,我需要一些帮助!在线~~
如果内容上显示(开),当我点击文件(除内容),内容将接近。
var $ = function(id){ return!id? null:document.getElementById(id); }
var addEventSimple = function(obj, evt, fn) { if (obj.addEventListener){ // W3C obj.addEventListener(evt, fn, false); }else if (obj.attachEvent) // Microsoft obj.attachEvent('on' + evt, fn); } var button = $('ppp'), content = $('content'); var init = function() {}; init.handleObj = button; init.showbox = content; init.flag = false; init.allowcls = false; init.open = function() {//open the content if (this.flag == false) { this.showbox.style.display = "block"; this.flag = true; this.allowcls = true; } }; init.close = function() { // close the content if (this.flag && this.allowcls) { this.showbox.style.display = "none"; this.flag = false; this.allowcls = false; } }; init.load = function() { // button click //e = e ||window.event; //e.preventDefault(); if (this.flag) { this.close(); } else { this.open(); } }; init.clickClose = function(e) { // document click if (!this.allowcls) { return; } e = e ||window.event; var target = e.target; if (target === this.showbox) { // button return; } this.close(); }; addEventSimple(button, 'click', function(){ init.load();//the code run here OK })// error on here,but i don't know why? addEventSimple(document, 'click', function (e){ init.clickClose(e); })
代码在这里:http://jsfiddle.net/DCty3/
您是否熟悉jQuery?如果你是,是什么原因,你没有使用它?它允许使用更少的代码来做更多的事情。无论如何,你的“问题”并不是真正的问题。 – Deele 2012-02-16 06:38:06
我明白jQuery,但javascript更重要,jquery无法做到全部。 – Terry 2012-02-16 06:45:12
jQuery IS JavaScript库。你给“问题”是微不足道的jQuery问题。 – Deele 2012-02-16 06:55:05