생성 된 새로운 특정 DOM 요소의 텍스트를 변경하고 싶다고 가정 해 보겠습니다. 예 :
(실제 API가 아니라는 것을 알고 있습니다)
$('body').on('creation', '.change-this', function() {
$(this).text('Boom!');
});
따라서 프로그래밍 방식으로 생성 <p class="change-this">
하고 DOM에 추가하면 (jQuery 또는 기타를 통해) 위의 내용이 실행됩니다. 이것이 jQuery가 할 수있는 일입니까? 내가 생각할 수있는 것은 프로그래밍 방식으로 HTML을 추가 $.trigger('creation')
하고 이벤트를 처리 할 핸들러를 준비하는 것입니다.
새 항목을 추적하고 싶습니까? 맞춤 이벤트를 사용하여이를 수행 할 수 있습니다. jQuery를 사용하고 있으므로이 메서드 .trigger()
를 사용하여 이벤트를 만들 수 있습니다.
예를 들어, 요소를 만들 때마다 이벤트를 트리거해야합니다. 따라서 코드에서 다음과 같이 추가합니다.
... // Your code goes here
new_item = '<p class="change-this">'; // The DOM that you will inject
container.append(new_item); // Injecting item into DOM
container.trigger('creation', []); // Firing a event signaling that a new item was created
...
그런 다음이 맞춤 이벤트에 대한 리스너를 만듭니다.
container.on('creation', function() {
console.log("Yay! New item added to container!");
});
이 스 니펫을 확인하십시오.
$('#add').on('click', function() {
var item = '<p>New item</p>',
container = $('#container');
container.append(item);
container.trigger('creation');
});
$('#container').on('creation', function () {
var new_message = 'New Item added!<br>';
$('#message').append(new_message);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="add">add</button>
<b id="message"></b>
<div id="container"></div>
사용자 지정 트랙으로 요소를 추적하려는 경우 다른 사용자 지정 이벤트를 만들고 해당 트리거를 처리하기 만하면됩니다.
그래서 그게 다입니다. jQuery는 사용자 지정 이벤트로 작업하는 간단하고 아름다운 방법을 제공합니다. 대박!
자세한 내용은 다음 링크를 확인 할 수 있습니다 jQuery.trigger를 () - http://api.jquery.com/trigger/
맞춤 이벤트 소개 -http : //learn.jquery.com/events/introduction-to-custom-events/
CustomEvent- https: //developer.mozilla.org/en-US/docs/Web/API/CustomEvent
행운을 빕니다!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다