【发布时间】:2016-02-05 10:39:40
【问题描述】:
最近我从这个论坛的另一个帖子中找到了以下 javascript;
var $content=$('div.leg_ol');
var $links=$('.leg_test').hover(function(){
var index= $links.index(this);
$content.stop(true,true).hide().eq(index).fadeIn();
},function(){
$content.stop(true,true).hide().eq(index);
});
(我会链接到 OP,但不幸的是已经丢失了页面)。
JSFIDDLE:https://jsfiddle.net/mfyctwvs/1/
代码完全符合我的要求 - 理论上,现在我对 js 几乎完全陌生,所以这对我来说是一个非常棘手的领域 - 请耐心等待。
当我在functions.php中发布代码时,它会导致我的整个网站停止工作,我认为是因为它无法读取它或存在一些冲突?
所以我的第一个想法是,查看 jsfiddle 是 js 版本,它被指定为 no wrap in 。如果我更改其中任何一个,代码都不起作用.. ..so 1. 我是否在尝试在我的 functions.php 中包含不兼容的 js 时犯了一个新错误(可能是的?)& 2. 我可以做一个简单的改变吗让它在我的functions.php中工作?
我已经为此搜索了好几个小时,并且确定我可以通过一些调整来解决这个问题吗?
仅供参考;函数.php
<?php// Set path to WooFramework and theme specific functions
$functions_path = get_template_directory() . '/functions/';
$includes_path = get_template_directory() . '/includes/';
// Don't load alt stylesheet from WooFramework
if ( ! function_exists( 'woo_output_alt_stylesheet' ) ) {
function woo_output_alt_stylesheet () {}
}
// WooFramework
require_once ( $functions_path . 'admin-init.php' ); // Framework Init
if ( get_option( 'woo_woo_tumblog_switch' ) == 'true' ) {
//Enable Tumblog Functionality and theme is upgraded
update_option( 'woo_needs_tumblog_upgrade', 'false' );
update_option( 'tumblog_woo_tumblog_upgraded', 'true' );
update_option( 'tumblog_woo_tumblog_upgraded_posts_done', 'true' );
require_once ( $functions_path . 'admin-tumblog-quickpress.php' ); // Tumblog Dashboard Functionality
}
/*-----------------------------------------------------------------------------------*/
$includes = array(
'includes/theme-options.php', // Options panel settings and custom settings
'includes/theme-functions.php', // Custom theme functions
'includes/theme-actions.php', // Theme actions & user defined hooks
'includes/theme-comments.php', // Custom comments/pingback loop
'includes/theme-js.php', // Load JavaScript via wp_enqueue_script
'includes/theme-plugin-integrations.php', // Plugin integrations
'includes/sidebar-init.php', // Initialize widgetized areas
'includes/theme-widgets.php', // Theme widgets
'includes/theme-advanced.php', // Advanced Theme Functions
'includes/theme-shortcodes.php', // Custom theme shortcodes
'includes/woo-layout/woo-layout.php', // Layout Manager
'includes/woo-meta/woo-meta.php', // Meta Manager
'includes/woo-hooks/woo-hooks.php' // Hook Manager
);
// Allow child themes/plugins to add widgets to be loaded.
$includes = apply_filters( 'woo_includes', $includes );
foreach ( $includes as $i ) {
locate_template( $i, true );
}
// Load WooCommerce functions, if applicable.
if ( is_woocommerce_activated() ) {
locate_template( 'includes/theme-woocommerce.php', true );
}
/*-----------------------------------------------------------------------------------*/
/* You can add custom functions below */
/*-----------------------------------------------------------------------------------*/
add_action( 'init', 'woo_custom_move_navigation', 10 );
function woo_custom_move_navigation () {
// Remove main nav from the woo_header_after hook
remove_action( 'woo_header_after','woo_nav', 10 );
// Add main nav to the woo_header_inside hook
add_action( 'woo_header_inside','woo_nav', 10 );
} // End woo_custom_move_navigation()
/* Testing stuff for mobile */
function woo_load_responsive_meta_tags () {
$html = '';
$html .= "\n" . '<!-- Always force latest IE rendering engine (even in intranet) & Chrome Frame -->' . "\n";
$html .= '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />' . "\n";
echo $html;
} // End woo_load_responsive_meta_tags()
add_action('wp_enqueue_scripts', function () {
wp_enqueue_script(
'user-scripts',
get_template_directory_uri() . '/functions/user-scripts.js',
array('jquery') // any script dependancies. i.e. jquery
);
});
?>
【问题讨论】:
-
"post the code in functions.php" php 脚本中的 javascript 会导致其中断。你能在php页面中展示你是如何使用js代码的吗?
-
Tbh 我以前从未这样做过,所以可能会犯各种错误,我尝试按原样添加它,就像您评论的那样导致它中断(学到的一课)尝试像 但这不起作用(我将脚本保存为 leg_hover.js)。我是否应该使用类似 echo '' ; ??
-
你在用wordpress吗?如果是这样,您需要inject the script into your page
-
从你所说的听起来我需要一个php函数来调用js?
-
你会在链接上有例子。我会发布答案
标签: javascript php wordpress