2017-01-05 45 views
0

在我的WordPress 4.7我已經包含了這樣一些自定義的JS:jQuery代碼只能在控制檯

function custom_scripts() { 

     wp_register_script ('custom', get_stylesheet_directory_uri() . '/js/custom.js', array('jquery'),'2.1.4',true); 

     wp_enqueue_script('custom'); 

} 

add_action('wp_enqueue_scripts', 'custom_scripts'); 

這是我custom.js內容:

jQuery(document).ready(function($) { 

    // This part works 
    $('#menu-main li a').on("click", function(){ 
     $("body").removeClass("overlay-open"); 
    }); 


    // This part does not work 
    $('th.bit-date').html('<th class="bit-date">New-Title</th>'); 
    $('th.bit-venue').html('<th class="bit-venue">New-Title</th>'); 
    $('th.bit-location').html('<th class="bit-location">New-Title</th>'); 
    $('th.bit-tickets').html('<th class="bit-tickets" colspan="4">New-Title</th>'); 

    // This console msg outputs from this script 
    console.log("Done!") 


}); 

只有的第一部分代碼正在運行,而不起作用的部分,在從控制檯執行時按預期工作。此代碼將與插件生成的表進行交互。在所有插件完全執行後,如何讓這個js代碼運行? (我打開其他的解決方案,以及)

+0

wp_enqueue_script(字符串$處理,字符串$ SRC = '',數組$ DEPS =陣列(),字符串|布爾|空$ ver = false,bool $ in_footer = false);設置依賴關係數組,它將確保您的腳本在之後加載。您的wp_register_script中存在相同的功能。找出哪個腳本構建您的表並將其添加到您的依賴關係中。 –

+0

'.ready(function($)'這是我第一次看到'帶有$ in的函數讀取的文檔',只是猜測試圖將其刪除 – KiRa

回答

2

試試這個:

jQuery(document).ready(function($) { 

    // This part works 
    $('#menu-main li a').on("click", function(){ 
     $("body").removeClass("overlay-open"); 
    }); 


    // This should work now 
    $(window).load(function() { 
     $('th.bit-date').html('<th class="bit-date">New-Title</th>'); 
     $('th.bit-venue').html('<th class="bit-venue">New-Title</th>'); 
     $('th.bit-location').html('<th class="bit-location">New-Title</th>'); 
     $('th.bit-tickets').html('<th class="bit-tickets" colspan="4">New-Title</th>'); 
    }); 
    // This console msg outputs from this script 
    console.log("Done!") 


}); 
+0

這解決了我的問題謝謝:) – Fjott

+0

@Fjott你非常歡迎!快樂編碼! –