a標簽調(diào)用js的方法總結
a標簽點擊事件方法匯總
<a> 標簽的 href 屬性用于指定超鏈接目標的 URL,href 屬性的值可以是任何有效文檔的相對或絕對 URL,包括片段標識符和 JavaScript 代碼段。
這里的href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" ,其中javascript:是偽協(xié)議,它可以讓我們通過一個鏈接來調(diào)用javascript函數(shù).而采用這個方式
javascript:;可以實現(xiàn)A標簽的點擊事件運行時,如果頁面內(nèi)容很多,有滾動條時,頁面不會亂跳,用戶體驗更好。
1. a href="javascript:js_method();" rel="external nofollow" rel="external nofollow"
這是平臺上常用的方法,但是這種方法在傳遞this等參數(shù)的時候很容易出問題,而且javascript:協(xié)議作為a的href屬性的時候不僅會導致不必要的觸發(fā)window.onbeforeunload事件,在IE里面更會使gif動畫圖片停止播放。W3C標準不推薦在href里面執(zhí)行
javascript語句
2. a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"
這種方法是很多網(wǎng)站最常用的方法,也是最周全的方法,onclick方法負責執(zhí)行js函數(shù),而void是一個操作符,void(0)返回undefined,地址不發(fā)生跳轉。而且這種方法不會像第一種方法一樣直接將js方法暴露在瀏覽器的狀態(tài)欄。
3.a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method()"
這種方法跟跟2種類似,區(qū)別只是執(zhí)行了一條空的js代碼。
4.a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method()"
這種方法也是網(wǎng)上很常見的代碼,#是標簽內(nèi)置的一個方法,代表top的作用。所以用這種方法點擊后網(wǎng)頁后返回到頁面的最頂端。
<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a> 如果頁面有滾動條 點擊后網(wǎng)頁后返回到頁面的最頂端
5.a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"
這種方法點擊執(zhí)行了js函數(shù)后return false,頁面不發(fā)生跳轉,執(zhí)行后還是在頁面的當前位置。
我看了下taobao的主頁,他們采用的是第2種方法,而alibaba的主頁是采用的第1種方法,和我們的區(qū)別是每個href里的javascript方法都用try、catch包圍。
綜合上述,在a中調(diào)用js函數(shù)最適當?shù)姆椒ㄍ扑]使用:
- a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"
- a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method()"
- a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;" 我們常用的在a標簽中有點擊事件:
- a href="javascript:js_method();" rel="external nofollow" rel="external nofollow"
以上就是關于a標簽調(diào)用js的全部知識點內(nèi)容,感謝大家對腳本之家的支持。
相關文章
javascript getElementsByClassName 和js取地址欄參數(shù)
為了從一大堆HTML代碼中找出我們的樹狀菜單(也許有多個),我們先來實現(xiàn)一個通過className找DOM節(jié)點的方法:getElementsByClassName。這是對瀏覽器自有DOM方法的一個簡單但實用的擴充。2010-01-01Bootstrap入門教程一Hello Bootstrap初識
Bootstrap,來自 Twitter,是目前很受歡迎的前端框架。Bootstrap是基于 HTML5、CSS3和Javascriopt開發(fā)的。這篇文章主要介紹了基于Bootstrap3實現(xiàn)漂亮簡潔的CSS3價格表(精美代碼版),需要的朋友可以參考下2017-03-03JS數(shù)組實現(xiàn)分類統(tǒng)計實例代碼
本文通過實例代碼給大家介紹了js數(shù)組實現(xiàn)分類統(tǒng)計的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09根據(jù)選擇不同的下拉值出現(xiàn)相對應的文本輸入框
根據(jù)用戶選擇不同的下拉值,出現(xiàn)相應的文本輸入框,在某些情況下比較實用,本文為大家寫了個,有需求的朋友可以參考下2013-08-08JavaScript中Async/Await通過同步的方式實現(xiàn)異步的方法介紹
在JavaScript的異步編程中,我們經(jīng)常使用回調(diào)函數(shù)、Promise和 Async/Await來解決異步操作的問題,Async/Await 又是Promise的語法糖,它的出現(xiàn)讓異步編程變得更加直觀和易于理解,本文將詳細講解Async/Await如何通過同步的方式實現(xiàn)異步2023-06-06概述javascript在Google IE中的調(diào)試技巧
本篇文章主要是對javascript在Google IE中的調(diào)試技巧進行了介紹,需要的朋友可以過來參考下2016-11-11