admin管理员组文章数量:1327999
I'm trying to make a widget plugin that allows user to upload image. but I have a problem in jQuery events that it is never happen !
includes/myplugin.scripts
function media_uploader_enqueue() {
wp_enqueue_media();
wp_register_script('media-uploader', plugins_url().'/simple-plugin/js/main.js',array('jquery'),null, true);
wp_enqueue_script('media-uploader');
}
add_action('admin_enqueue_scripts', 'media_uploader_enqueue');
js/main.js
jQuery(document).ready(function ($) {
console.log($('#xyz')); // works !
$('#xyz').click(function (e) {
e.preventDefault();
console.log("in click event"); // never happens !!
})
})
I'm trying to make a widget plugin that allows user to upload image. but I have a problem in jQuery events that it is never happen !
includes/myplugin.scripts
function media_uploader_enqueue() {
wp_enqueue_media();
wp_register_script('media-uploader', plugins_url().'/simple-plugin/js/main.js',array('jquery'),null, true);
wp_enqueue_script('media-uploader');
}
add_action('admin_enqueue_scripts', 'media_uploader_enqueue');
js/main.js
jQuery(document).ready(function ($) {
console.log($('#xyz')); // works !
$('#xyz').click(function (e) {
e.preventDefault();
console.log("in click event"); // never happens !!
})
})
Share
Improve this question
asked Jul 20, 2020 at 12:25
Islam Hanafi MahmoudIslam Hanafi Mahmoud
1616 bronze badges
2
- 2 This seems like it's specifically a jquery question about how to fire your jquery event, so as it's not related to Wordpress it might do better on a Web Development stackexchange, or StackOverflow. If you leave the question here you might want to add the HTML of the xyz element too – mozboz Commented Jul 20, 2020 at 13:00
- 1 I found the answer and I think this is related to wordpress, thought, thanks for your suggestion @mozboz – Islam Hanafi Mahmoud Commented Jul 20, 2020 at 13:16
1 Answer
Reset to default 3I Found the answer for my question , And I'm sharing it here in case anyone interested.
the problem was that click event won't work on dynamically generated html elements
and the alternative that worked is using on
$('body').on('click','#elementID',Callback)
本文标签: pluginsjQuery Click event is not fired at all
版权声明:本文标题:plugins - jQuery Click event is not fired at all 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742248191a2440251.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论