javascript - 无法让javascript和Jquery在functions.php中工作
问题描述
这是我的第一篇文章!
我对编码有点陌生,我知道足够多的 html 和 css,并使用诸如 bootstrap 之类的框架来开发一个基本的网站,并且已经开始学习如何使用 php 在 wordpress 中构建主题。
我的目标是使用 bootstrap 和 javascript 构建一个主题,但我在执行最简单的任务时遇到了麻烦,例如让脚本在 functions.php 中工作。我一直在网上关注一些教程,但这些教程不起作用,经过大量的实验和研究,我似乎无法得到任何工作。
我的functions.php代码是:
<?php
function styles() {
wp_register_style('bootstrap', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css' );
$dependencies = array('bootstrap');
wp_enqueue_style( 'bootstrapstarter-style', get_stylesheet_uri(), $dependencies );
wp_register_style('style', get_template_directory_uri() . 'style.css');
$dependencies = array('style');
wp_enqueue_style('style', get_stylesheet_uri(), $dependencies);
}
add_action( 'wp_enqueue_scripts', 'styles' );
function scripts() {
wp_deregister_script('jquery3.4.1');
wp_enqueue_script('jquery3.4.1', get_template_directory_uri() . 'jquery-3.4.1.min.js', '', 1, true);
$dependencies = array('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri() .'/bootstrap/js/bootstrap.min.js', $dependencies, '3.3.6', true );
wp_enqueue_script('customjs', get_template_directory_uri() . 'scripts.js', '', 1, true);
}
add_action( 'wp_enqueue_scripts', 'scripts' );
function title(){
add_theme_support( 'title-tag' );
}
add_action( 'after_setup_theme', 'title' );
?>
我有一段测试代码,它在 scripts.js 中显示一个弹出警报(并且页脚正在调用 ?php wp_footer();? ):
$(document).ready(function(){
alert('test');
})
我还尝试过加载顺序,认为引导 js 可能会干扰我自己的 js,但无论我如何定位它们,我都无法让它们工作。
抱歉,如果这是一个愚蠢的问题,并且之前已经问过这个问题,但我所查找的任何内容似乎都不起作用,任何帮助将不胜感激。
非常感谢!
解决方案
您可能必须用 jQuery 替换 JavaScript 中的 $ 。jQuery 在 WP 中默认处于兼容模式。这意味着 jQuery 的典型 $ 快捷方式不起作用。
jQuery(document).ready(function(){
alert('test');
})
推荐阅读
- html - 如何修改引导程序的卡片列?
- javascript - 在 addEventListener 函数中给出时 removeEventListener 不起作用
- c - 我的老师做了这个功能,但我不明白它有什么用
- java - 如何使用列表实现深度优先遍历测试?
- python - 从列表中计算并将输出打印为表格
- r - r - Average timestamps excluding the day
- python - 使用scrapy抓取需要身份验证的网站
- c++ - //初始化一个新的由p指向的INTSTK,它最多可以存储m个整数
- sql - 如何在 Google BigQuery SQL 中做 LAG & LEAD?
- prestashop-1.7 - prestashop1.7 下拉列表中的 ps_mainmeu 不起作用