javascript - 我无法将 JavaScript 应用于 WordPress。你能帮助我吗?

标签 javascript jquery wordpress

我按照本教程进行操作:https://www.youtube.com/watch?v=t0swZkgTQnk 解释了如何将 JavaScript 添加到 WordPress。但是当我启动页面时,在源页面中,.js 文件被重新定位到其他位置。

我将 .js 文件放在“C:\Users\xxxxx\Documents\Websites\www.testsite.dev.cc\wp-content\themes\profile-lite\js”中,但在页面源中,该文件列在“http://www.testsite.dev.cc/wp-content/themes/profile-lite/style.cssjs/script.js?ver=3.2.1”中。

我尝试在“C:\Users\xxxxx\Documents\Websites\www.testsite.dev.cc\wp-content\themes\profile-lite”中创建一个文件夹,其中包含文件夹“style.cssjs”以查看如果文件可以读取但没有任何运气!

在我的 function.php 文件中,这是我的代码:

function cool_scripts(){
    
    wp_enqueue_script('cool-stuff', get_stylesheet_uri(). 'js/script.js', array('jquery'), '3.2.1', false);
}
    add_action('wp_enqueue_scripts','cool_scripts');

在我的 script.js 文件中,这是我的代码:

//with class div
        jQuery(document).ready(function(){
            $(".videowidth").hover(function(){
                $(this).children("video")[0].play();
            },
            function(){
            $(this).children("video")[0].pause();
            });
        });
        // Without id and class div
        /*$(document).ready(function(){
            $("video").hover(function(){
                $(this)[0].play();
            },
            function(){
            $(this)[0].pause();
            });
        });*/

我希望加载该文件,但控制台中出现此错误。

(索引):103 GET http://www.testsite.dev.cc/wp-content/themes/profile-lite/style.cssjs/script.js?ver=3.2.1 net::ERR_ABORTED 404(未找到)

enter image description here

最佳答案

我找到了!!

(function($) {
    $(function() {
        $('video').hover(function() {
            this.play();
        }, function() {
            this.pause()
        });
    });
})(jQuery);

关于javascript - 我无法将 JavaScript 应用于 WordPress。你能帮助我吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56495432/

相关文章:

javascript - 没有让这个主干路由器工作

javascript - Aptana 替代方案

javascript - ng-show 在 js 文件中不起作用

javascript - 仅为可见元素插入编号

javascript - jQuery - 通过 Id 获取动态 (*) 元素并放入数组

javascript - "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"

jquery - 在提交输入之前增加 Bootstrap 标签输入的大小

javascript - 调整背景图像以适合浏览器窗口 JavaScript/HTML/CSS/JQuery

html - 在不失去响应能力的情况下删除子主题中的 CSS 参数

javascript - 将两个不同的 <div> 标签附加到两个不同的 <image> 标签