How can I make this script to run on Wordpress?

2019-03-07 00:31发布

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!

2条回答
老娘就宠你
2楼-- · 2019-03-07 00:48

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

$(document).ready(function(){
    $(".text1").click(function(){
        $(this).hide();
    });
});
查看更多
Emotional °昔
3楼-- · 2019-03-07 00:59

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();
    });
});
查看更多
登录 后发表回答