【发布时间】:2014-05-29 07:12:00
【问题描述】:
我从另一个 stackoverflow 问题中复制并粘贴了此脚本。这是他们提供的 JSfiddle。 http://jsfiddle.net/LmVRZ/2/
我有一个 wordpress 网站,我使用 wp_enqueue_script 和 wp_register_script 链接它,这是文件 –
var sourceSwap = function () {
var $this = $(this);
var newSource = $this.data('alt-src');
$this.data('alt-src', $this.attr('src'));
$this.attr('src', newSource);
}
$(function () {
$('img.switch').hover(sourceSwap, sourceSwap);
});
它说这个错误“Uncaught TypeError: undefined is not a function on simple script. (anonymous function).
不知道我做错了什么,因为我是直接复制粘贴的??
如果有帮助,以下是我在 wordpress 中加载脚本的方法
wp_register_script( 'rollover', get_template_directory_uri() . '/js/rollover.js' , array( 'jquery' ) );
【问题讨论】:
-
你能粘贴你的 HTML 吗?
-
检查jquery是否加载成功
-
只是出于好奇,您是否尝试过正常的脚本调用(不使用 WP 注册脚本的东西)?有时 CMS' 希望您指定什么是脚本类型,例如:
-
通常使用 wordpress 你需要用 jquery 包装你的代码 noconflict 类似 jQuery(document).ready(function($) {