jQuery 就绪功能在 WordPress 中不起作用

问题描述 投票:0回答:4

我在 WordPress(@主页)上使用 jQuery,但 Ready 函数对我不起作用。我有一个index.php,其中包括(php)页眉、页脚和侧边栏。我测试了这段代码:

<script type="text/javascript" src="path_to_jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
   $(document).ready(function() {
      alert ("test text");
   });
</script>

警报(带有文本

"test text"
)只是没有立即弹出!它只会在我的侧边栏加载后才会弹出。这意味着当我看到索引页(侧边栏尚未加载)时,我必须等待几秒钟,直到侧边栏完成加载,然后才执行 jQuery 代码:弹出警报。所以ready函数就不起作用了。 谁能告诉我为什么以及如何解决这个问题?谢谢。

wordpress
4个回答
64
投票

在 WordPress 环境中,使用以下命令:

jQuery(function($){
    // now you can use jQuery code here with $ shortcut formatting
    // this executes immediately - before the page is finished loading
});


jQuery(document).ready(function($){
    // now you can use jQuery code here with $ shortcut formatting
    // this will execute after the document is fully loaded
    // anything that interacts with your html should go here
}); 

2
投票

侧边栏加载后会弹出警报,因为 read() 应该在整个页面加载完成后执行。


1
投票

警报(带有“测试文本”文本)并没有立即弹出!它只会在我的站点栏加载后才会弹出。

这正是使用ready

优势
。当您希望它立即弹出时,只需这样做

<script type="text/javascript">
  alert ("test text");
</script>

0
投票

也许有很多方法可以做到。我会告诉你我的方法(经过测试),所以...... 在典型的 WordPress 主题中,您至少应该有如下文件: index.php 和functions.php(我重复一遍!-最小,因为它应该更多,更多,就像search.php,single.php 等等,等等..)。

上面的每个文件都应该包含 html 和 php 代码以及 css,以实现良好的前端,但这里我将只关注最必要的 php 和 html,所以这里是:

index.php的内容

<!DOCTYPE html>
<html <?php language_attributes(); ?>>    
    <?php get_header(); ?>
        <body <?php body_class(); ?>>    
            <!-- here add Your html and php code of index.php -->
            <?php
    
            // Here is the place all magic come true.. get_footer (wp_footer)
            // where..
            // get_footer() will call any JAVASCRIPTS code in right way, but
            // don't forget inform about it functions.php file in his right section.
            // BTW: get_footer() will call wp_footer, and this is most important 
            // information for functions.php file, because while wp_footer will call, 
            // then add_action function will run, 
            // and will set up correctly all javascript code inside WordPress Core
            // (please look into functions.php file)
            //
            // PLEASE:
            // DO NOT ADD ANY JAVASCRIPT CODE HERE (at the end of index file) - 
            // - it could work, but it is not recommended
            //
            get_footer(); 
            ?>               
        </body>
</html>

现在..假设您的主题名为:Michael_theme

functions.php 的内容可能类似于:

<?php
    add_action( 'after_setup_theme', 'Michael_theme_setup' );
    function Michael_theme_setup() 
        {
        register_nav_menus( array( 'main-menu' => esc_html__( 'Main Menu', 'Michael_theme' )));
        }

    // loading HEADER scripts by right WordPress way
    add_action( 'wp_enqueue_scripts', 'Michael_theme_enqueue' );
    function Michael_theme_enqueue() 
        {
        wp_enqueue_style( 'Michael_theme', get_stylesheet_uri() );   // loading css scripts
        wp_enqueue_script( 'jquery' );                              // loading jlquery.js (one of js scripts)

        // here is another option to add Your own javascript code in js file if You wish
        }
    
    // loading FOOTER scripts by WordPress way
    add_action( 'wp_footer', 'Michael_theme_footer' );
    function Michael_theme_footer() 
        {
        <script>
            jQuery(document).ready(function($)
                {
                alert("test text"); // here is Your text to show
                }
        </script>
        }
    
    if ( !function_exists( 'Michael_theme_wp_body_open' ) ) 
        {
        function Michael_theme_wp_body_open() { do_action( 'wp_body_open' ); }
        }
?>

您还可以使用例如“get_template_part()”WordPress 函数来分割代码,并将每个部分打包到单独的 php 文件中。 我的意思是你可以在functions.php文件中调用get_template_part,但是..恐怕现在没有必要解释这一切。如果您愿意,请询问。

© www.soinside.com 2019 - 2024. All rights reserved.