How can I make this script to run on Wordpress?

2019-03-07 00:47发布

问题:

I have this script:

$(document).ready(function(){
    $("text1").click(function(){
        $(this).hide();
    });
});

Code html:

 <div class="div1">
    <p class="text1">text to appear when the user puts the mouse over</p>
</div>

I want to apply this script to my website in Wordpress.

I put this code in functions.php

  add_action( 'wp_enqueue_scripts', 'script_echipa' );
function script_echipa() 

{ wp_enqueue_script( 'script', get_template_directory_uri() . '/js/echipa.js', array('jquery'), null, true );

}

It is a very simple script that works smoothly on local Wordpress but we did not implement it.

Is something wrong?

Can you help me to solve this problem?

Thanks in advance!

回答1:

You forgot to add a . before the selector for class text1 --> "text1"

$(document).ready(function(){
    $(".text1").click(function(){
        $(this).hide();
    });
});


回答2:

WordPress uses noconflict so you have to wrap your scripts in jQuery(function($) {}); in order to be able to use the dollar sign. Like so:

jQuery(function($) {
    $(".text1").click(function() {
        $(this).hide();
    });
});