【问题标题】:Uncaught TypeError: undefined is not a function on simple script未捕获的类型错误:未定义不是简单脚本上的函数
【发布时间】: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($) {

标签: jquery wordpress image


【解决方案1】:

尝试做:

jQuery(function ($) {
    var sourceSwap = function () {
        var $this = $(this);
        var newSource = $this.data('alt-src');
        $this.data('alt-src', $this.attr('src'));
        $this.attr('src', newSource);
    }
    $('img.switch').hover(sourceSwap, sourceSwap);
});

【讨论】:

    猜你喜欢
    • 2015-03-14
    • 2014-12-13
    • 2014-08-19
    • 2014-05-25
    相关资源
    最近更新 更多