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 は、ファイルが 1 つであろうと数千であろうと関係ありません。単純に DOM (1 つの .php ファイルまたは含まれる 1000 個の .php ファイルによって作成される可能性があります) を検索して、一致する要素を探します。

この問題をデバッグするための私のアドバイスは次のとおりです。まず、my_js.jsによって生成されたページに が存在することを確認しother.phpます。wp_enqueue_script

于 2013-09-03T10:24:01.850 に答える