jquery - 在 wordpress 中正确放置 $(document).ready(function(){ 的位置?

标签 jquery css wordpress include

我正在尝试添加 library到 wordpress。 所以我已经将所有的 CSS 和 JavaScript 库添加到 Wordpress。 现在我必须运行一些将在调用以下函数时执行的 Java 代码。

$(document).ready(function(){
        if (Modernizr.touch) {
            // show the close overlay button
            $(".close-overlay").removeClass("hidden");
            // handle the adding of hover class when clicked
            $(".img").click(function(e){
                if (!$(this).hasClass("hover")) {
                    $(this).addClass("hover");
                }
            });
            // handle the closing of the overlay
            $(".close-overlay").click(function(e){
                e.preventDefault();
                e.stopPropagation();
                if ($(this).closest(".img").hasClass("hover")) {
                    $(this).closest(".img").removeClass("hover");
                }
            });
        } else {
            // handle the mouseenter functionality
            $(".img").mouseenter(function(){
                $(this).addClass("hover");
            })
            // handle the mouseleave functionality
            .mouseleave(function(){
                $(this).removeClass("hover");
            });
        }
    });

如何将其添加到我的页面?

最佳答案

您应该将此代码添加到一个文件中并使用 wp_enqueue_scripts 添加。
函数 scripts_styles() {

使用wp_enqueue_script添加javascript

wp_enqueue_script( 'script-id', get_template_directory_uri() . '/js/main.js', array( 'jquery' ));

使用wp_enqueue_style

添加CSS wp_enqueue_style( 'all-theme', get_template_directory_uri() . '/css/style.css.css');
} add_action( 'wp_enqueue_scripts', 'scripts_styles' );

关于jquery - 在 wordpress 中正确放置 $(document).ready(function(){ 的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33121448/

相关文章:

html - 如何在 block 元素旁边嵌入视频

html - Wordpress hover css 菜单是什么意思?

javascript - CSS - 将元素符号添加到事件选项卡、垂直对齐和留在选项卡内问题

php - AJAX 从 PHP 响应更改 div 内容不起作用

jquery - 在ajax中设置全局变量获得成功

javascript - 我如何检查 X 个具有相同类但不同 id 的元素是否有一个特定的 css 属性相同?

html - 如何使用CSS移动div的位置

php - wordpress:header.php 和 footer.php 的默认位置

php - 几秒钟后突出显示不同的链接

javascript - 为什么单击菜单时我的轮播消失了?