jQuery代码仅适用于Console

时间:2017-01-05 22:05:37

标签: jquery wordpress

在我的Wordpress 4.7中,我已经包含了一些这样的自定义js:

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代码运行? (我也愿意接受其他解决方案)

1 个答案:

答案 0 :(得分: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!")


});