我本來以爲這是一個WordPress的問題 - 而是嘗試小時後實現 - 這可能是一個比較普遍的JavaScript問題在jQuery函數正確輸出的JavaScript變量
我一直在掙扎這几几小時,我希望你能指出我的方向。我一直在使用this教程來幫助我理解wp_localize_script
是如何工作的(這是一個WordPress函數,它允許您將PHP變量傳遞給JS文件)。首先,我排隊我的腳本,例如:
function of_load_scripts() {
if (!is_admin()) {
if(is_front_page()) {
global $post;
wp_register_script('supersized.shutter', get_template_directory_uri().'/js/mylibs/supersized.shutter.min.js', 'jquery');
wp_enqueue_script('supersized.shutter');
wp_register_script('shutter_home', get_template_directory_uri().'/js/shutter-home.js', 'jquery');
wp_enqueue_script('shutter_home');
}
$tester = get_field('text');
// Add some parameters for the JS.
wp_localize_script(
'shutter_home',
'shutter',
array(
'testerjs' => $tester
)
);
}
}
add_action('wp_print_scripts', 'of_load_scripts');
我已經驗證這是輸出到網頁的HTML源代碼:
<script type='text/javascript'>
/* <![CDATA[ */
var shutter = {"testerjs":"This is a test value"};
/* ]]> */
</script>
然後,在我的shutter-home.js
文件,我有以下幾點:
jQuery(document).ready(function($) {
var testerjs = shutter.testerjs;
alert(testerjs);
$.supersized({
// Functionality
slideshow : 1, // Slideshow on/off
autoplay : 1, // Slideshow starts playing automatically
start_slide : 1, // Start slide (0 is random)
stop_loop : 0, // Pauses slideshow on last slide
random : 0, // Randomize slide order (Ignores start slide)
slide_interval : 3000, // Length between transitions
transition : 1, // 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
transition_speed : 700, // Speed of transition
new_window : 1, // Image links open in new window/tab
pause_hover : 0, // Pause slideshow on hover
keyboard_nav : 1, // Keyboard navigation on/off
performance : 0, // 0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
image_protect : 0, // Disables image dragging and right click with Javascript
// Size & Position
min_width : 0, // Min width allowed (in pixels)
min_height : 0, // Min height allowed (in pixels)
vertical_center : 1, // Vertically center background
horizontal_center : 1, // Horizontally center background
fit_always : 0, // Image will never exceed browser width or height (Ignores min. dimensions)
fit_portrait : 1, // Portrait images will not exceed browser height
fit_landscape : 0, // Landscape images will not exceed browser width
// Components
slide_links : 'blank', // Individual links for each slide (Options: false, 'num', 'name', 'blank')
thumb_links : 1, // Individual thumb links for each slide
thumbnail_navigation : 0, // Thumbnail navigation
// Components
slides : [ // Slideshow Images
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-1.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-1.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/kazvan-3.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-3.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-1.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-1.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/wojno-3.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/wojno-3.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-1.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-1.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-2.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-2.jpg'},
{image : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/slides/shaden-3.jpg', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/shaden-3.jpg'}
],
// Theme
progress_bar : 0 // Slide bar that shows duration until next transition
});
});
這只是正常輸出This is a test value
(因爲我做了一個快速alert()
)。我遇到的問題是將此值傳遞給$.supersized
函數(具體位於image
位置的slides
區域中)。這可能是一個普通的JS問題 - 但是我該怎麼去堅持在那個區域內插入這個變量呢?我試過這樣的東西:
{image : ''+ testerjs + '', thumb : 'http://buildinternet.s3.amazonaws.com/projects/supersized/3.2/thumbs/kazvan-1.jpg'},
沒有骰子。任何幫助將不勝感激!
testerjs
僅僅是一個例子變量 - 它通常會舉行一個圖像URL - 所以只是用於舉例的目的
如果您的警報顯示正確的值,應該不會有問題。雖然沒有必要與空字符串連接。 – 2011-12-30 14:02:11
嗨@PhilParsons - 我想我正在尋找的是一個快速片段,我應該在'{image:''+ testerjs +''中使用,thumb:'http://buildinternet.s3.amazonaws.com/projects /supersized/3.2/thumbs/kazvan-1.jpg'}'應該正確地輸出該變量(因爲我不確定語法,並且已經在互聯網上搜索了很長時間,但沒有運氣)。該變量的初始化應該在'.supersized()'函數內嗎?謝謝! – Zach 2011-12-30 14:15:54
Bleh我很笨 - 這是因爲我正在輸出'This is a test value',而不是產生意外結果的實際圖像URL。好的 - 一個更加相關的問題可以添加到這裏(讓我知道如果我應該開始一個新的問題) - 你能傳遞一個PHP函數給JS嗎?所以 - 在這個例子中,我有一個我想用implode()來分別設置每張幻燈片的值的數組。謝謝! – Zach 2011-12-30 14:23:54