溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

詳解a標(biāo)簽添加onclick事件的幾種方式

發(fā)布時(shí)間:2020-09-29 15:43:04 來源:腳本之家 閱讀:143 作者:manny-liu 欄目:web開發(fā)

我們常用的在a標(biāo)簽中有點(diǎn)擊事件:

1. a href="javascript:js_method();" rel="external nofollow"

這種方法在傳遞this等參數(shù)的時(shí)候很容易出問題,而且javascript:協(xié)議作為a的href屬性的時(shí)候不僅會(huì)導(dǎo)致不 必要的觸發(fā)window.onbeforeunload事件,在IE里面更會(huì)使gif動(dòng)畫圖片停止播放。W3C標(biāo)準(zhǔn)不推薦在href里面執(zhí)行 javascript語句

2. a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"

這種方法是很多網(wǎng)站最常用的方法,也是最周全的方法,onclick方法負(fù)責(zé)執(zhí)行js函數(shù),而void是一個(gè)操作符,void(0)返回undefined,地址不發(fā)生跳轉(zhuǎn)。而且這種方法不會(huì)像第一種方法一樣直接將js方法暴露在瀏覽器的狀態(tài)欄。

3.a href="javascript:;" 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" onclick="js_method()"

這種方法也是網(wǎng)上很常見的代碼,#是標(biāo)簽內(nèi)置的一個(gè)方法,代表top的作用。所以用這種方法點(diǎn)擊后網(wǎng)頁后返回到頁面的最頂端。

5.a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"

這種方法點(diǎn)擊執(zhí)行了js函數(shù)后return false,頁面不發(fā)生跳轉(zhuǎn),執(zhí)行后還是在頁面的當(dāng)前位置。

綜合上述,在a中調(diào)用js函數(shù)最適當(dāng)?shù)姆椒ㄍ扑]使用:
a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" onclick="js_method()"

a href="javascript:;" rel="external nofollow" rel="external nofollow" onclick="js_method()"

a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" onclick="js_method();return false;"

以上所述是小編給大家介紹的a標(biāo)簽添加onclick事件的幾種方式詳解整合,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)億速云網(wǎng)站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI