【问题标题】:Wordpress insert javascript, error with $Wordpress 插入 javascript,$ 错误
【发布时间】:2016-04-03 05:36:12
【问题描述】:

我有一个 wordpress 页面。我想插入一个错字的javascript。我阅读了其他类似的问题,并尝试像他们那样做,但它仍然不起作用。 在页面上,控制台窗口显示错误:Uncaught TypeError: $(...).typed is not a function 并显示$("#slidertext").typed({ 部分错误。我是不是拼错了什么?

这是插入文件的header.php

$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/typo/insertjs.php";
include_once($path);

还有insertjs.php 文件:

<script src="/typo/typed.js" type="text/javascript"></script>
<script>
jQuery(document).ready(function($){

    $("#slidertext").typed({
        //strings: ["Luxus.", "Extravagancia.", "Profizmus.", "Try it  out!"],
        stringsElement: $('#texts'),
        typeSpeed: 30,
        backDelay: 500,
        loop: false,
        contentType: 'html', // or text
        // defaults to false for infinite loop
        loopCount: false,
        callback: function(){ foo(); },
        resetCallback: function() { newTyped(); }
    });

    $(".reset").click(function(){
        $("#slidertext").typed('reset');
    });

});

function newTyped(){ /* A new typed object */ }

function foo(){ console.log("Callback"); }
</script>

<div id="texts">
<p>Luxus.</p>
<p>Extravagancia.</p>
<p>Profizmus.</p>
</div>

typed.jsthis 非常相似。

【问题讨论】:

    标签: javascript wordpress typeerror


    【解决方案1】:

    您的 typed.js 未加载。请给出完整路径:

    <script src="/typo/typed.js" type="text/javascript"></script>
    

    喜欢

    <script src="<?php echo get_template_directory_uri(); ?>/typo/typed.js" type="text/javascript"></script>
    

    【讨论】:

    • 如果是本地主机呢?使用绝对 URL 加载资源不是一个好主意。她应该更好地找到正确的相对 url。但是脚本似乎没有加载,我同意这一点。
    • 我现在改成相对网址了。
    • 不幸的是,没有一个解决方案有效。但我很担心jquery.js 的问题可能是什么?在我看到 typed.js 的地方,他们在 index.html 上调用了 &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; 但是当我在 insertjs.php 中调用它时,Wonderplugin Slider 消失了。错误:Uncaught TypeError: jQuery(...).wonderpluginslider is not a functionUncaught TypeError: $(...).typed is not a function 也许正确的问题是如何调用jquery.min.js
    • 请尝试使用 $(...).wonderpluginslider 代替 jQuery(...).wonderpluginslider
    • @AtifTariq 不幸的是,这两种方法都不起作用。 =(错误:Uncaught TypeError: $ is not a function 这是以前的样子:jQuery(".wonderpluginslider").wonderpluginslider(); 我在这里将jQuery 更改为$。在wonderpluginsliged.js jQuery 中可以找到总共 23 个。如果我更改它们中的每一个错误是:wonderpluginslider.js 中的Uncaught TypeError: Cannot read property 'fn' of undefined(anonymous function)
    【解决方案2】:

    尝试无冲突:

    $=jQuery.noConflict();
    

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多