最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

a標(biāo)簽調(diào)用js的方法總結(jié)

 更新時(shí)間:2019年09月05日 16:20:03   作者:小豆芽博客  
在本篇文章中小編給大家整理了一篇關(guān)于a標(biāo)簽調(diào)用js的幾種方法的相關(guān)內(nèi)容,有此需要的朋友們可以參考下。

a標(biāo)簽點(diǎn)擊事件方法匯總

<a> 標(biāo)簽的 href 屬性用于指定超鏈接目標(biāo)的 URL,href 屬性的值可以是任何有效文檔的相對(duì)或絕對(duì) URL,包括片段標(biāo)識(shí)符和 JavaScript 代碼段。

這里的href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" ,其中javascript:是偽協(xié)議,它可以讓我們通過(guò)一個(gè)鏈接來(lái)調(diào)用javascript函數(shù).而采用這個(gè)方式

javascript:;可以實(shí)現(xiàn)A標(biāo)簽的點(diǎn)擊事件運(yùn)行時(shí),如果頁(yè)面內(nèi)容很多,有滾動(dòng)條時(shí),頁(yè)面不會(huì)亂跳,用戶體驗(yàn)更好。

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

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

javascript語(yǔ)句

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

<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" ></a> 如果頁(yè)面有滾動(dòng)條 點(diǎn)擊后網(wǎng)頁(yè)后返回到頁(yè)面的最頂端

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

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

我看了下taobao的主頁(yè),他們采用的是第2種方法,而alibaba的主頁(yè)是采用的第1種方法,和我們的區(qū)別是每個(gè)href里的javascript方法都用try、catch包圍。

綜合上述,在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" 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標(biāo)簽中有點(diǎn)擊事件:
  • a href="javascript:js_method();" rel="external nofollow" rel="external nofollow"

以上就是關(guān)于a標(biāo)簽調(diào)用js的全部知識(shí)點(diǎn)內(nèi)容,感謝大家對(duì)腳本之家的支持。

相關(guān)文章

最新評(píng)論

新津县| 石家庄市| 集安市| 崇信县| 武强县| 盘山县| 利川市| 礼泉县| 虎林市| 铁岭县| 葫芦岛市| 桦甸市| 福州市| 岢岚县| 南安市| 荔波县| 永胜县| 同德县| 离岛区| 鹤庆县| 墨竹工卡县| 通州市| 化德县| 姚安县| 沂源县| 嘉定区| 秭归县| 余干县| 上高县| 双流县| 兴城市| 柳江县| 滨州市| 临泉县| 广南县| 呼和浩特市| 万山特区| 萍乡市| 崇文区| 闽侯县| 仲巴县|