2015-11-18 56 views
0

我在這裏找到了這段偉大的代碼:(http://jsfiddle.net/bzBzL/) 我想在我的Wordpress頁面中使用它(我可能需要它在一頁以上)。我完全不熟悉Javascript和Wordpress Codex在這方面沒有多大幫助。 (https://codex.wordpress.org/Using_Javascript) 我需要在哪裏插入下面的代碼? 謝謝你們。 我的網站:www.visionmars.com在wordpress頁面中加載javascript

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); 
}); 
+0

jQuery代碼將無法正常工作與jQuery的附帶的WordPress。請參閱[關於jQuery noConflict Wrappers的文檔](http://codex.wordpress.org/Function_Reference/wp_enqueue_script#jQuery_noConflict_Wrappers)。 –

回答

0

你需要做一個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文件可以在主題根目錄中的任意位置創建。你可以創建一個文件夾並將文件放在那裏。

或者,如果你想要一個管理方式的角度來看你的管理面板Appearence>編輯器,在這裏找到footer.php並將<script></script>之間的代碼</body>結束標記之前。它應該將該腳本加載到您網站的所有頁面中。

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