2013-10-31 69 views
1

$我建立一個wordpress(v。3.6)網站使用二十十三主題的兒童主題。 我的主題的header.php文件具有

<body <?php body_class(); ?> onResize="resizeHandler();"> 

這是主題的.js文件,myChildTheme.js:

var myStuff; 
function resizeHandler() { 
    myStuff = jQuery(".stuff"); 
    console.log("resized, and here's stuff: "+ myStuff); 
} 

爲了使用$來代替「jQuery的」我嘗試了我的解決方案在網上找到。新myChildTheme.js:

jQuery(document).ready(function($) { 
    var myStuff; 
    function resizeHandler() { 
     myStuff = $(".stuff"); 
     console.log("resized, and here's stuff: "+ myStuff); 
    } 
}); 

但後來我得到一個控制檯引用錯誤,說resizeHandler沒有定義。

我以正確的方式入選並註冊了主題的腳本和樣式。

的functions.php:

function myChildTheme_scripts_styles() { 
    wp_enqueue_style('myChildTheme-fonts', myChildTheme_fonts_url(), array(), null); 
} 
add_action('wp_enqueue_scripts', 'myChildTheme_scripts_styles'); 

function myChildTheme_scripts() { 
    wp_enqueue_script('myChildTheme_functions', get_stylesheet_directory_uri().'/js/myChildTheme.js', array('jquery'), '', true); 
    wp_enqueue_script('isotope_source', get_stylesheet_directory_uri().'/js/jquery.isotope.min.js', array('jquery'), '', true); 
} 
add_action('wp_enqueue_scripts', 'myChildTheme_scripts'); 

我如何能得到$任何提示工作?謝謝。

+0

'.ready(function($){'''''你可以使用'''而不是'jQuery' – Joren

+0

通過將代碼包裝在'document.ready'處理程序中,可以減少它的可見性。可以看到它,另外,我沒有看到你試圖在任何地方使用'$' – Archer

+0

對不起,我忘了使用'$'的正確示例,我現在糾正了它 – maguskrool

回答

2

您需要通過jQuery本身附加resize事件。試試這個:

<body <?php body_class(); ?>> 
jQuery(document).ready(function($) { 
    var myStuff; 
    function resizeHandler() { 
     myStuff = $(".stuff"); // note $ use here 
     console.log("resized, and here's stuff: "+ myStuff); 
    } 

    $(window).resize(resizeHandler); 
}); 

FYI控制檯將輸出resized, and here's stuff: [Object object] - 這是正常的。

+0

謝謝,它工作完美! – maguskrool