如何让这个脚本在Wordpress上运行?

时间:2015-03-12 19:46:57

标签: php jquery html wordpress

我有这个脚本:

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

代码html:

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

我想将此脚本应用到我在Wordpress中的网站。

我把这段代码放在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 );

}

这是一个非常简单的脚本,可以在本地Wordpress上顺利运行,但我们没有实现它。

有问题吗?

你能帮我解决这个问题吗?

提前致谢!

2 个答案:

答案 0 :(得分:1)

你忘了添加一个。在类text1的选择器之前 - &gt; &#34;文本1&#34;

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

答案 1 :(得分:1)

WordPress使用noconflict,因此您必须将脚本包装在jQuery(function($) {});中才能使用美元符号。像这样:

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