『壹』 js實現滑鼠滑過顯示二級菜單
滑鼠滑過哪顯示啥?
滑過 產品介紹 顯示下面的子菜單?
onmouseover是滑鼠移動到某元素執行的滑鼠事件。
onmousemove是滑鼠在某元素上移動執行的事件。
你先把這兩個分清楚了,根據你的需求是
先獲取你要滑鼠滑過的元素 也就是產品介紹這個a元素,你給其設置一個id假如設置其為proct
那麼先獲取他
var proct=document.getElementById('proct');
再獲取你要顯示的子菜單元素proctInfo
var proctInfo=document.getElementById('proctInfo');
proctInfo.style.display='none';//設置其隱藏,如果CSS里已經隱藏,此步可以省略。
然後設置其滑鼠事件,應該選擇onmouseover
proct.onmouseover=function(){
proctInfo.style.display='block'; //滑鼠移動到proct元素上讓其子菜單顯示。
}
proct.onmouseout=function(){
proctInfo.style.display='block'; //滑鼠移出proct元素上讓其子菜單隱藏。
}
如果你要讓每個子菜單都讀取他的子菜單可以循環遍歷實現。思路相同。
『貳』 用javascript寫一個單擊菜單展開子菜單,單擊其他菜單關閉已展開的子菜單,展開當前菜單的子菜單;
<style>
.menu{bgcolor:#000;width:100px;height:100px;border:1px solid #000}
</style>
<div>
<h2 onclick=menu(1)>1</h2><div class="menu" id="menu_1" style="none"></div>
<h2 onclick=menu(2)>2</h2><div class="menu" id="menu_2" style="none"></div>
<h2 onclick=menu(3)>3</h2><div class="menu" id="menu_3" style="none"></div>
<h2 onclick=menu(4)>4</h2><div class="menu" id="menu_4" style="none"></div>
</div>
<script>
function menu(i)
switch(i){
case 1:
document.getElementById("menu_1").style="block";
document.getElementById("menu_2").style="none";
document.getElementById("menu_3").style="none";
document.getElementById("menu_4").style="none";
break;
case 2:
document.getElementById("menu_1").style="none";
document.getElementById("menu_2").style="block";
document.getElementById("menu_3").style="none";
document.getElementById("menu_4").style="none";
break;
case 3:
document.getElementById("menu_1").style="none";
document.getElementById("menu_2").style="none";
document.getElementById("menu_3").style="block";
document.getElementById("menu_4").style="none";
break;
case 4:
document.getElementById("menu_1").style="none";
document.getElementById("menu_2").style="none";
document.getElementById("menu_3").style="none";
document.getElementById("menu_4").style="block";
}
</script>
估計不怎麼好看,要好看就是css的問題了
『叄』 JS單擊顯示/隱藏子菜單代碼
你定義的a是全局變數,不管你點擊的菜單是菜單一,還是菜單二都執行,不要這樣寫,直接加判斷,點擊菜單一時如果display是block就隱藏,否則顯示
註:火狐和ie獲取display的方法不同,用前先判斷,要不不兼容
var
currentstyle
=
function(e){
return
e.currentstyle
||
document.defaultview.getcomputedstyle(e,
null);
}
if(currentstyle(document.getelementbyid(id))['display']=='none'){
document.getelementbyid(divname).style.display
=
"block";
}else{
document.getelementbyid(divname).style.display
=
"none";
}
『肆』 js怎麼樣使左邊顯示菜單右邊顯示內容左邊顯示菜單欄 右邊顯示內容 菜單欄要可以有子菜單
這是在你那個flash中設置...不是跑到網頁上來設置
在網頁上來設置也可以,但要用復雜的JS來判斷滑鼠坐標,以及考慮寬窄屏和兼容性等等....不推薦
『伍』 js怎麼獲取當前子菜單的top值和left 值
首先要取得top值和left值,先要把這個div的定位設為
relative
或者
absolute
比如:
<div
id="tiao"
style="margin-top:
3;margin-left:
-5;width:
470;height:
25;position:relative"
></div>
top值:
document.getelementbyid('tiao').offsettop+document.body.scrolltop
left值:document.getelementbyid('tiao').offsetleft;
『陸』 在js中怎麼做到一列菜單,當滑鼠移上去的時候會顯示子菜單,離開時隱藏子菜單
滑鼠移動上去用【onmouseover】,此時子菜單使用【display=「block」】屬性顯示;滑鼠離開用【onmouseout】,此時子菜單使用【display=「none」】隱藏;