Programing

jQuery에서 live ()를 on ()으로 바꾸기

lottogame 2020. 5. 10. 10:16
반응형

jQuery에서 live ()를 on ()으로 바꾸기


내 응용 프로그램이 동적으로 드롭 다운을 추가했습니다. 사용자는 필요한만큼 추가 할 수 있습니다.

나는 전통적으로 jQuery의 live()방법을 사용하여 이러한 드롭 다운 중 하나가 언제 발견되었는지 감지했습니다 change().

$('select[name^="income_type_"]').live('change', function() {
    alert($(this).val());
});

jQuery 1.7부터는 다음과 같이 업데이트했습니다.

$('select[name^="income_type_"]').on('change', function() {
    alert($(this).val());
});

문서를 보면 완벽하게 유효해야합니다 (맞습니까?). 그러나 이벤트 핸들러는 절대 실행되지 않습니다. 물론 jQuery 1.7이로드되어 실행되고 있음을 확인했습니다. 오류 로그에 오류가 없습니다.

내가 무엇을 잘못하고 있지? 감사!


on문서 (굵게) 상태) :

이벤트 핸들러는 현재 선택된 요소에만 바인드됩니다. 코드가 호출 할 때 페이지에 존재해야합니다 .on().

동등한에이 .live()같은 것

$(document.body).on('change', 'select[name^="income_type_"]', function() {
    alert($(this).val());
});

이벤트 핸들러를 가능한 한 요소, 즉 계층 구조에서 더 가까운 요소에 가깝게 바인딩하는 것이 좋습니다.

업데이트 : 다른 질문에 대답하는 동안 .live문서 에도 언급되어 있음을 알았습니다 .

.live()후속 방법 으로이 방법을 다시 작성하는 것은 간단합니다. 다음은 세 가지 이벤트 첨부 메소드 모두에 대한 동등한 호출을위한 템플리트입니다.

$(selector).live(events, data, handler);                // jQuery 1.3+
$(document).delegate(selector, events, data, handler);  // jQuery 1.4.3+
$(document).on(events, selector, data, handler);        // jQuery 1.7+

선택한 답변 외에도

jQuery.live애플리케이션이 마이그레이션되기를 기다리는 동안 jQuery 1.9 이상으로 포트 하십시오. 이것을 JavaScript 파일에 추가하십시오.

// Borrowed from jQuery 1.8.3's source code
jQuery.fn.extend({
  live: function( types, data, fn ) {
          if( window.console && console.warn ) {
           console.warn( "jQuery.live is deprecated. Use jQuery.on instead." );
          }

          jQuery( this.context ).on( types, this.selector, data, fn );
          return this;
        }
});

참고 : 위 기능 this.selector은 제거 된 jQuery v3에서 작동하지 않습니다 .

또는 https://github.com/jquery/jquery-migrate 를 사용할 수 있습니다


Just found a better solution which doesn't involve editing third party code:

https://github.com/jquery/jquery-migrate/#readme

Install the jQuery Migrate NuGet package in Visual Studio to make all the versioning issues go away. Next time Microsoft update their unobtrusive AJAX and validation modules perhaps try it without the migrate script again to see if they resolved the issue.

As jQuery Migrate is maintained by the jQuery Foundation I think this is not only the best approach for third party libraries and also to get warning messages for your own libraries detailing how to update them.


In addition to the selected answers,

If you use Visual Studio, you can use the Regex Replace.
In Edit > Find and Replace > Replace in Files
Or Ctrl + Shift + H

In Find and Replace pop-up, set these fields

Find what: \$\((.*)\)\.live\((.*),
Replace with: $(document.body).on($2,$1,
In find options check "Use Regular Expressions"


jquery verision x.x.x.js open editor, find jQuery.fn.extend

add code

live: function( types, data, fn ) {
        jQuery( this.context ).on( types, this.selector, data, fn );
        return this;
    },

Example : jquery-2.1.1.js --> line 7469 (jQuery.fn.extend)

Example images view

참고URL : https://stackoverflow.com/questions/8021436/turning-live-into-on-in-jquery

반응형