我正在使用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!' );
});
答案 0 :(得分:1)
$(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]);
});