2

this指向问题,标签上绑定onclick事件this指向问题

 2 years ago
source link: https://blog.p2hp.com/archives/9067
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

this指向问题,标签上绑定onclick事件this指向问题 | Lenix Blog

<input type="button" value="修改" οnclick="changeContent()">

写在元素上onclick里面的函数changeContent,相当于函数直接调用,函数里面使用的this指向全局对象window,

而不是指向该元素

那么要获取onclick所在节点的object,我们则需要在调用function时就把this传递过去:

<input type="button" value="修改" οnclick="changeContent(this)">

这里的this指这个input标签,$(this)将其变为jq对象;

function changeContent(e){

var _this = $(this)

标签: javascript
 123
45678910
11121314151617
18192021222324
25262728293031
« 6月    

归档

日志总数:2019篇 评论总数:170条
分类总数:26个 标签总数:320个
友情链接:9个 网站运行:3812天
最后更新:2022年7月13日

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK