js選擇並轉移導覽功能表範例程式碼,js導覽功能表範例程式碼

來源:互聯網
上載者:User

js選擇並轉移導覽功能表範例程式碼,js導覽功能表範例程式碼

實現html介面

<!DOCTYPE html><html><head><title>Select and Go Navigation</title><script src="script01.js"></script><link rel="stylesheet" href="script01.css" rel="external nofollow" ></head><body><form action="gotoLocation.cgi" class="centered"><select id="newLocation"><option selected>Select a topic</option><option value="script06.html">Cross-checking fields</option><option value="script07.html">Working with radio buttons</option><option value="script08.html">Setting one field with another</option><option value="script09.html">Validating Zip codes</option><option value="script10.html">Validating email addresses</option></select><noscript><input type="submit" value="Go There!"></noscript></form></body></html>

實現菜單導航

window.onload = initForm;window.onunload = function() {};function initForm() {document.getElementById("newLocation").selectedIndex = 0;document.getElementById("newLocation").onchange = jumpPage;}function jumpPage() {var newLoc = document.getElementById ("newLocation");var newPage = newLoc.options [newLoc.selectedIndex].value;if (newPage != "") {window.location = newPage;}}

下面是源碼分析
1.

window.onload = initForm;
window.onunload = function() {};
在視窗載入時,調用initForm()函數。下一行需要解釋一下,因為它是處理某些瀏覽器的古怪行為的變通方法。

當視窗卸載時(即關閉視窗或者瀏覽器轉到另一個網址),我們調用一個匿名函數(anonymousfunction),即沒有名稱的函數。在這個樣本中,這個函數不但沒有名稱,而且根本不做任何事情。提供這個函數是因為必須將onunload設定為某種東西,否則,當單擊瀏覽器的back按鈕時,就不會觸發onload事件,因為在某些瀏覽器(比如Firefox和Safari)中頁面會被緩衝。讓onunload執行任何操作,就會使頁面不被緩衝,因此當使用者後退時,會發生onload事件。

匿名是指在function和()之間沒有名稱。這是觸發onunload但不讓它做任何事情的最簡單的方法。與任何函數中一樣,花括弧包含函數的內容。這裡的花括弧是空的,因為這個函數不做任何事情。

2.

document.getElementById("newLocation").selectedIndex = 0;
document.getElementById("newLocation").onchange = jumpPage;
在initForm()函數中,第一行獲得HTML頁面上的菜單(它的id為newLocation),並且將它的selectedIndex屬性設定為零,這會使它顯示Select a topic。
第二行讓指令碼在菜單選擇發生改變時,調用jumpPage()函數。

3.

var newLoc = document.getElementById("newLocation");
在jumpPage()函數中,newLoc變數尋找訪問者在菜單中選擇的值。

4.

var newPage = newLoc.options[newLoc.selectedIndex].value;
從方括弧中的代碼開始,向外依次分析。newLoc.selectedIndex是一個從0~5的數字(因為有6
個菜單選項。記住JavaScript的編號常常是基於零的)。得到這個數字之後,接下來獲得對應的功能表項目
的值,這就是我們希望跳轉到的網頁的名稱。然後,將結果賦值給變數newPage。

5.

if (newPage != "") {
window.location = newPage;
這個條件陳述式首先檢查newPage是否非空。換句話說,如果newPage有一個值,那麼讓視窗轉到
選擇的功能表項目所指定的URL。


js 導覽功能表代碼,

tabCon = $_class("sub_zzjs").getElementsByTagName("ul");
所以,tabCon是ul元素的一個集合。
tabCon[o]是集合中的第o個元素。
 
急需導航二級菜單的js原始碼?

這裡有一個js的多級聯動下拉式功能表
可以自訂位置和樣式 比較實用
裡面有教程和源碼
參考資料:www.blueidea.com/...=11931
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.