最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
jQuery ajax使用$(this).parent()无效解决方法
时间:2022-11-14 22:03:19 编辑:袖梨 来源:一聚教程网
html
| 代码如下 | 复制代码 |
|
|
需求说明:
鼠标点击‘remove’链接,根据ajax的返回值删除页面元素。
无效的方法
| 代码如下 | 复制代码 |
| $('.remove').bind('click',function(){ $.ajax({ type:'post', url:$(this).attr('href'), dataType : 'json', data:{id : $(this).attr('id')}, success:function(msg){ if(msg.error==0){ alert(msg.msg); }else{ $(this).parent().remove(); //此处无法获得父级元素 } } }); return false; }); |
|
有效的方法
| 代码如下 | 复制代码 |
| $('.remove').bind('click',function(){ div=$(this).parent(); //先获取父级元素 $.ajax({ type:'post', url:$(this).attr('href'), dataType : 'json', data:{id : $(this).attr('id')}, success:function(msg){ if(msg.error==0){ alert(msg.msg); }else{ div.remove(); //再删除 } } }); return false; }); |
|
其他类似问题也可以通过相同方法解决
相关文章
- 方块英雄传好玩吗 方块英雄传玩法简介 05-27
- 《Forza Horizon 6》媒体评分汇总 05-27
- ARC Raiders:如何完成迷失在传输中的任务 05-27
- 勇者幻兽录:各平台福利礼包对比分析哪家渠道更超值 05-27
- Debian Strings:系统更新操作指南 05-27
- 网易云音乐电脑版如何解锁灰色版权歌曲-亲测可用的网易云音乐电脑版灰色版权歌曲解锁教程 05-27