jQuery jquery-1.7.1.min.js live()不建议使用on() [英] JQuery jquery-1.7.1.min.js live() deprecated use on()

查看:106
本文介绍了jQuery jquery-1.7.1.min.js live()不建议使用on()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自jQuery网站:

from jQuery website:

从jQuery 1.7开始,不推荐使用.live()方法.使用.on()来 附加事件处理程序.

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers.

在1.7.1版中,我尝试将所有live()更改为on(),但没有一个起作用.有谁知道为什么吗?

with version 1.7.1 i tried to change all my live() to on(), but none worked. Does anyone has any idea why?

这是怎么称呼的:

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>

这是不起作用的脚本之一:

and this is one of the scripts that don't work:

$(".toBeSaved [col=ISRC] input").on('change',function() {
        var pid = $(this).parent().parent().attr('primary_key');
        $("[primary_key="+pid+"] [col=isrc_id] input").val('');
        $("[primary_key="+pid+"] [col=isrc_id] input").css({'border':'1px solid red','background-color':'#e8b7cf'});
    });

html:

<tr primary_key="44" class="toBeSaved">
<td col="ISRC" style="width: 91px; " class="editableCell"><input class="editableInput auto" type="text" undefined=""></td>
<td col="LineYear" style="width: 35px; " class="editableCell"><input class="editableInput  " type="text"></td>
<td col="isrc_id" style="width: 41px; " class="editableCell"><input class="editableInput undefined" type="text" undefined="" readonly="readonly"></td></tr>

我能问一下-为什么是"-1"吗?我的问题恰好有什么问题?

and can i just ask - why "-1"?? what exactly is wrong with my question?

推荐答案

将代码从使用.live转换为.on不仅仅是将对.live的调用替换为.on的情况.它们接受不同的参数,并在不同的选择器上被调用.例如,旧语法:

Converting code from using .live to .on isn't just a case of replacing the calls to .live with .on calls. They accept different arguments, and are called on different selectors. For example, the old syntax:

$('a').live('click', function () {});

使用.on:

$(document).on('click', 'a', function () {});

此语法可为您提供更大的控制力和灵活性.

This syntax gives you greater control and flexibility.

我建议阅读文档: http://api.jquery.com/on/

有关从.live转换为.on的信息: http://api.jquery.com/live/

And for information on converting to .on from .live: http://api.jquery.com/live/

这篇关于jQuery jquery-1.7.1.min.js live()不建议使用on()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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