JQuery jquery-1.7.1.min.js live() deprecated use on()

*爱你&永不变心* 提交于 2019-12-02 05:27:50

问题


from jQuery website:

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

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


this is how it gets called:

<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>

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


回答1:


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 () {});

With .on:

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

This syntax gives you greater control and flexibility.

I would recommend reading the documentation: http://api.jquery.com/on/

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




回答2:


Specific to the code submitted, as per the ramblings above;

Replace

$(".toBeSaved [col=ISRC] input").on('change',function() {

with

 $(document).on('change','.toBeSaved [col=ISRC] input', function() {


来源:https://stackoverflow.com/questions/9805307/jquery-jquery-1-7-1-min-js-live-deprecated-use-on

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!