如何在javascript / jquery中设置以在特定事件中挂钩特定功能,例如wordpress动作挂钩如何工作
基本代码
$('body').on('click', '[data-modal]', function() {
var dis = $(this).data('modal');
// how can I hook something in here from outside this file
$('#'+dis).addClass('is-active');
// I also want to hook and run a function in here without directly calling them
});
以下是我想要实现的目标
$('body').on('click', '[data-modal]', function() {
var dis = $(this).data('modal');
trigger('before_open_' + dis); // I don't know what method to use here
$('#'+dis).addClass('is-active');
trigger('after_open_' + dis); // I don't know what method to use here
});
// I don't also know how to properly initialize the event
$(document).on('before_open_classes_pop', function() {
console.log( $(this).data('modal') );
});
任何帮助将不胜感激
答案 0 :(得分:0)
首先创建custom events
$( "[data-modal]" ).on( "before_open", function(){
var dis = $(this).modal();
//logic to handle the before open event for a specific dis here
});
和
$( "[data-modal]" ).on( "after_open", function(){
var dis = $(this).modal();
//logic to handle the after open event for a specific dis here
});
现在以
的形式调用这些事件$('body').on('click', '[data-modal]', function() {
var dis = $(this).data('modal');
$(this).trigger('before_open');
$('#'+dis).addClass('is-active');
$(this).trigger('after_open');
});