Я не могу применить JavaScript к Wordpress.Вы можете мне помочь? - PullRequest
0 голосов
/ 07 июня 2019

Я следовал этому уроку по адресу: 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 (не найдено) введите описание изображения здесь

Ответы [ 2 ]

1 голос
/ 07 июня 2019

Я нашел это !!

(function($) {
    $(function() {
        $('video').hover(function() {
            this.play();
        }, function() {
            this.pause()
        });
    });
})(jQuery);
0 голосов
/ 07 июня 2019

Вам нужно использовать get_stylesheet_directory_uri() вместо get_stylesheet_uri().

function cool_scripts(){

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

См. этот ответ для получения дополнительной информации о том, выберите ли get_template_directory_uri или get_stylesheet_directory_uri.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...