JQuery jquery-1.7.1.min.js live()不建议使用on()

Ele*_*len 2 jquery live

来自jQuery网站:

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

在版本1.7.1中,我试图将我的所有live()更改为on(),但没有一个工作.有谁知道为什么?


这就是它被调用的方式:

<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

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

$(".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'});
    });
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

我可以问 - 为什么"-1"?什么究竟是错我的问题?

cal*_*rae 7

使用转换代码.live.on不只是更换调用的情况下.live.on电话.它们接受不同的参数,并在不同的选择器上调用.例如,旧语法:

$('a').live('click', function () {});
Run Code Online (Sandbox Code Playgroud)

.on:

$(document).on('click', 'a', function () {});
Run Code Online (Sandbox Code Playgroud)

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

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

而对于上转换为信息.on来自.live: http://api.jquery.com/live/

  • 你显然*没有*阅读文档,它有一个非常明确的部分转换为.on,非常明确地说你在文档对象上调用它.我现在正在为我的答案添加一个代码示例. (2认同)