[英]Trigger a custom event Jquery
我正在使用jQuery插件,並嘗試在主體中插入jQuery Dom元素后觸發自定義事件。但是事件從未發生。
如果沒有用戶交互,這可能嗎?
我的代碼是:
var $div = $( '<div />' ), $container = $div.clone();
$container.text( 'test' ).appendTo( 'body' );
$.event.trigger( 'ContainerInit', [ $container ]);
$( document ).on( 'ContainerInit', function( e, $elementContainer ) {
alert( 'test here!' );
});
$(document).ready(function() {
var $div = $('<div />'),
$container = $div.clone();
$container.text('test').appendTo('body');
// you should provide a selector to the handler!
$(document).on('ContainerInit', 'div', function(e, $elementContainer) {
// console.log is better than alert
console.log('test here!');
});
// trigger event only after assigning handler using correct way to trigger!
$container.trigger('ContainerInit', [$container]);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.