get the value of “onclick” with jQuery?

前端 未结 6 683
耶瑟儿~
耶瑟儿~ 2020-12-09 15:16

Is it possible to get the current value of the onClick attribute of an A tag via jQuery?

For example, I have:



        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 15:55

    I'm not quite sure how to do this in jQuery... but this works:

    var x = document.getElementById('google').attributes;
    for (var i in x) {
     if (x[i].name == "onclick") alert(x[i].firstChild.data);
    }
    

    but like Harshath said it would be better if you used event listeners, as removing and adding this function back into the onclick event may be troublesome.

提交回复
热议问题