簡體   English   中英

在WordPress頁面中加載JavaScript

[英]load javascript in wordpress pages

我在這里找到了這段很棒的代碼:( http://jsfiddle.net/bzBzL/ ),我想在我的Wordpress頁面中使用它(我可能需要一頁以上的頁面)。 我對Java完全陌生,而Wordpress Codex在這一方面並沒有多大幫助。 https://codex.wordpress.org/Using_Javascript )我需要在哪里插入以下代碼? 多謝你們。 我的網站:www.visionmars.com

var timer, fullText, currentOffset, onComplete;

function Speak(person, text, callback) {
    $("#name").html(person);
    fullText = text;
    currentOffset = 0;
    onComplete = callback;
    timer = setInterval(onTick, 100);
}

function onTick() {
    currentOffset++;
    if (currentOffset == fullText.length) {
        complete();
        return;
    }
    var text = fullText.substring(0, currentOffset);
    $("#message").html(text);
}

function complete() {
    clearInterval(timer);
    timer = null;
    $("#message").html(fullText);
    onComplete();
}

$(".box").click(function () {
    complete();
});

Speak("Simon",
    "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod         tempor incididunt ut labore et dolore magna aliqua.",

function () {
    setTimeout(function(){
    Speak("Javascript", "Simon has finished speaking!");
    }, 2000);
});

您需要制作一個JavaScript文件,然后可以將其加載到functions.php中,如下所示:

function theme_name_scripts() {
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

或在footer.php中,如果您的文件位於模板所在主題的根目錄中,如下所示:

<script src='<?php echo THEME_URL; ?>/your_js_file.js'></script>

可以在主題根目錄中的任何位置創建JavaScript文件。 您可以制作一個文件夾,然后將文件放在該文件夾中。

或者,如果您希望以管理方式看待,則可以轉到“管理面板”中的“外觀”>“編輯器”,在此處找到footer.php,並將代碼放在</body>關閉標記之前的</body> <script></script>之間。 並且應該將該腳本加載到您網站的所有頁面中。

另外,完整的文檔在這里https://codex.wordpress.org/Function_Reference/wp_enqueue_script

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM