在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/HTML/ 為什么總是獲取不到a標(biāo)簽的自定義屬性?

為什么總是獲取不到a標(biāo)簽的自定義屬性?


<a class="button button-glow button-border button-rounded button-primarybtn-xs" onclick="clickBtn();return false;"  data-href="/cancel/2">取消</a>


    function  clickBtn() {
        var data_href=$(this).attr("data-href");
        alert(data_href);//undefined
    }

請(qǐng)問(wèn) 為什么獲取不到a標(biāo)簽的自定義屬性???

回答
編輯回答
夢(mèng)一場(chǎng)

jquery有現(xiàn)成的data方法不用,非要去用attr....

<a id="demo" data-href="test">取消</a>

$("#demo").on('click', function(){
    console.log($(this).data("href"));
});

極其不推薦使用onclick()這樣的方式寫(xiě)點(diǎn)擊事件,其他事件也是一樣的,耦合太高,可維護(hù)性極低。
極其不推薦使用onclick()這樣的方式寫(xiě)點(diǎn)擊事件,其他事件也是一樣的,耦合太高,可維護(hù)性極低。
極其不推薦使用onclick()這樣的方式寫(xiě)點(diǎn)擊事件,其他事件也是一樣的,耦合太高,可維護(hù)性極低。

2017年8月10日 10:14
編輯回答
心上人

你需要通過(guò)JQuery綁定事件監(jiān)聽(tīng)器才能使用$(this)獲取目標(biāo)元素:

<a id="click" data-href="11">取消</a>

$('#click').click(function  clickBtn() {
    var data_href=$(this).attr("data-href");
    console.log(data_href);//11
})
2017年5月12日 00:14
編輯回答
離魂曲

原生的js事件和jQuery的事件混寫(xiě)的錯(cuò)誤,因?yàn)閖Query對(duì)象和dom對(duì)象是不一樣的,如果你非得這樣混寫(xiě),就要實(shí)現(xiàn)兩種不同的對(duì)象的轉(zhuǎn)換

2018年3月18日 23:29
編輯回答
柚稚

問(wèn)題已經(jīng)解決,漏掉了參數(shù)



<a class="button button-glow button-border button-rounded button-primarybtn-xs" onclick="clickBtn(this);return false;"  data-href="/cancel/2">取消</a>


    function  clickBtn(dom) {
        var data_href=$(dom).attr("data-href");
        alert(data_href);//undefined
    }
2017年6月19日 21:10