.on()jquery不工作 [英] .on() jquery not working

查看:139
本文介绍了.on()jquery不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

jQuery - 将.live()转换为.on()

JQuery'on'vs.'live'

在jQuery中将live()转换成on()


我知道.live()已被弃用,但是.on()让我有些错误。
当我动态生成圆顶时,它不起作用,它只能在第一次点击时起作用。而.live()相同的代码正常工作。我使用jquery 1.8



工作代码

  $(' .item')。live('click',function(e){
alert('test');
// ajax调用重新生成.item元素
});

此代码仅适用于首次点击:



($)$($。$)$('。item')。on('click',function(e){
alert('test');
// ajax call重新生成.item元素
});

有什么问题?

解决方案

检查此处的文档 .on 略有不同:

  $(document).on click,.item,function(event){
alert('test');
// ajax调用重新生成.item元素
});


Possible Duplicate:
jQuery - convert .live() to .on()
JQuery ‘on’ vs. ‘live’
Turning live() into on() in jQuery

I know .live() is deprecated, but .on() is getting me some errors. It does not work when I dynamically generate the dome, it only works at the first click. Whilst the same code with .live() works correctly. I'm using jquery 1.8

Working code

$('.item').live('click', function(e) {
   alert('test');
   // ajax call that regenerates .item element
});

This code only works at the first click:

$('.item').on('click', function(e) {
   alert('test');
   // ajax call that regenerates .item element
});

What's wrong?

解决方案

Check the documentation here. .on works slightly different:

$(document).on("click", ".item", function(event){
    alert('test');
    // ajax call that regenerates .item element
});

这篇关于.on()jquery不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆