0

I'm working on a Wordpress plugin and a I have to use jquery. In my plugin file (call it 'my_p.php') I register my .js file (my_js.js) in this way:

add_action('admin_enqueue_scripts', 'my_script');
function my_script() {
    wp_register_script( 'my_jquery', plugins_url( '/js/my_js.js', __FILE__ ), array( 'jquery' ), '1.0.0', false );
    wp_enqueue_script( 'my_jquery' );
}

Code in my_js.js is:

 jQuery(document).ready(function($){
         jQuery(document.body).on('click','#button_my_p', function(e){
          alert('hello!');
    });
});

my problem is the following: if 'button_my_p' is an element (in my case a button) in *my_p.php* file, it works fine and I see the alert when I click on the button. But, if 'button_my_p' is an element of another .php file (for example 'other.php' in my plugin folder), jquery code doesn't work.. I don't see the alert..

What am I missing???

My plugin's structure is:

  • js folder in which I put my_js.js.
  • my_p.php file
  • other.php file

Thanks for your help!

4

1 回答 1

0

jQuery 不在乎你有一个文件还是一千个文件。它只是在 DOM(可能由一个 .php 文件或一千个包含的文件创建)中搜索匹配的元素。

我对调试此问题的建议是:首先,检查my_js.js生成的页面上是否存在other.php- 如果不是 - 那么你使用wp_enqueue_script不正确

于 2013-09-03T10:24:01.850 回答