最近用得一个效果,如图:
先看结构.
XML/HTML代码
- <div class="NSerBox clear">
- <div id="SerBoxClass" class="SerBox" onclick="Onclick('Class');">
- <p id="input_Class">选择分类</p>
- </div>
- <ul class="filetree" id="TreeBoxClass" style="display:none;" onmouseout="aiout('Class')" onmouseover="aiover('Class')">
- <li><a href="#" onclick="ok('Class',this)">笔记本1</a></li>
- <li><a href="#" onclick="ok('Class',this)">笔记本2</a></li>
- <li><a href="#" onclick="ok('Class',this)">笔记本3</a></li>
- <li><a href="#" onclick="ok('Class',this)">笔记本4</a></li>
- <li><a href="#" onclick="ok('Class',this)">笔记本5</a></li>
- </ul>
- </div>
主要考虑到p的宽度,以及用背景时能更好的处理CSS,而用不着去另写结构.
当然,看到那么多onclick,其实主要由于个人JS不过关导致的,见谅!
下面是JS.拼在一起能运行没出错.呵呵
一、考虑用setTimeout与clearTimeout实现鼠标移开隐藏ul. Demo
JavaScript代码
- <!--
- function Onclick(id){
- var aide = document.getElementById("TreeBox"+id).getElementsByTagName("li");
- for (i=0;i<aide.length;i++) {
- aide[i].onmouseover = function (){
- this.style.backgroundColor="#c0e5f2";
- }
- aide[i].onmouseout = function (){
- this.style.backgroundColor="#fff";
- }
- }
- if (document.getElementById("TreeBox"+id).style.display == 'none'){
- document.getElementById("TreeBox"+id).style.display = "block";
- }
- else{
- document.getElementById("TreeBox"+id).style.display = "none";
- }
- return false;
- }
- function aiover(id){
- try{window.clearTimeout(clearTime);}catch(e){}
- }
- function aiout(id){
- var aide=document.getElementById("TreeBox"+id).style.display;
- if(aide=="block"){
- clearTime = setTimeout("ainone('Class')", 1000);
- }
- }
- function ainone(id){
- document.getElementById("TreeBox"+id).style.display="none";
- }
- function ok(id,href)
- {
- var onone = document.getElementById("TreeBox"+id);
- document.getElementById("input_"+id).innerHTML=href.innerHTML;
- onone.style.display="none";
- }
- //-->
二、考虑用点击SerBox外的其它区域时隐藏. Demo 别外在p 里加了 input ....
JavaScript代码
- <!--
- function Onclick(id){
- var aide = document.getElementById("TreeBox"+id).getElementsByTagName("li");
- for (i=0;i<aide.length;i++) {
- aide[i].onmouseover = function (){
- this.style.backgroundColor="#c0e5f2";
- }
- aide[i].onmouseout = function (){
- this.style.backgroundColor="#fff";
- }
- }
- if (document.getElementById("TreeBox"+id).style.display == 'none'){
- document.getElementById("TreeBox"+id).style.display = "block";
- }
- else{
- document.getElementById("TreeBox"+id).style.display = "none";
- }
- return false;
- }
- function ok(id,tname){
- var onone = document.getElementById("TreeBox"+id);
- document.getElementById("input_"+id).value=tname;
- onone.style.display="none";
- }
- document.onclick = function(e){
- var event = window.event || e;
- var noe = event.srcElement || event.target;
- if(noe.className !== "SerBox"){
- document.getElementById("TreeBoxClass").style.display="none";
- //alert('看点击的是那....');
- }
- else{
- return;
- }
- }
- //-->
其它的不说,有错请告之,谢


