我需要帮助实现一个脚本代码到PHP文件

时间:2016-02-18 作者:Duy

我需要帮助在PHP文件中实现一个Javascript代码,用一个新的搜索框替换默认的搜索框。我必须使用echo吗?看起来怎么样?提前谢谢你的帮助,我已经试了好几个小时了。

$(function () {
    $(\'a[href="#search"]\').on(\'click\', function(event) {
        event.preventDefault();
        $(\'#search\').addClass(\'open\');
        $(\'#search > form > input[type="search"]\').focus();
    });

    $(\'#search, #search button.close\').on(\'click keyup\', function(event) {
        if (event.target == this || event.target.className == \'close\' || event.keyCode == 27) {
            $(this).removeClass(\'open\');
        }
    });
实现此PHP:

<?php
//Header File
if ( ! defined( \'ABSPATH\' ) ) exit;
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( \'charset\' ); ?>">
<?php
wp_head();
?>
</head>
<body <?php body_class(); ?>>
<div id="global" class="global">
    <div class="pagesidebar">
        <div class="sidebarcontent">    
            <h2 id="sidelogo">
                <a href="<?php echo vibe_site_url(); ?>"><img src="<?php  echo apply_filters(\'wplms_logo_url\',VIBE_URL.\'/assets/images/logo.png\',\'pagesidebar\'); ?>" alt="<?php echo get_bloginfo(\'name\'); ?>" /></a>
            </h2>
            <?php
                $args = apply_filters(\'wplms-mobile-menu\',array(
                    \'theme_location\'  => \'mobile-menu\',
                    \'container\'       => \'\',
                    \'menu_class\'      => \'sidemenu\',
                    \'fallback_cb\'     => \'vibe_set_menu\',
                ));

                wp_nav_menu( $args );
            ?>
        </div>
        <a class="sidebarclose"><span></span></a>
    </div>  
    <div class="pusher">
        <?php
            $fix=vibe_get_option(\'header_fix\');
        ?>
        <header class="sleek transparent <?php if(isset($fix) && $fix){echo \'fix\';} ?>">
            <div class="<?php echo vibe_get_container(); ?>">
                <div id="searchdiv">
                    <form role="search" method="get" id="searchform" action="<?php echo home_url( \'/\' ); ?>">
                        <div><label class="screen-reader-text" for="s">Search for:</label>
                            <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" placeholder="<?php _e(\'Hit enter to search...\',\'vibe\'); ?>" />
                            <?php 
                                $course_search=vibe_get_option(\'course_search\');
                                if(isset($course_search) && $course_search)
                                    echo \'<input type="hidden" value="course" name="post_type" />\';
                            ?>
                            <input type="submit" id="searchsubmit" value="Search" />
                        </div>
                    </form>
                </div>
                <div class="row">
                    <div class="col-md-12 col-sm-12 col-xs-12">
                        <?php

                            if(is_home()){
                                echo \'<h1 id="logo">\';
                            }else{
                                echo \'<h2 id="logo">\';
                            }
                        ?>

                            <a href="<?php echo vibe_site_url(); ?>"><img src="<?php  echo apply_filters(\'wplms_logo_url\',VIBE_URL.\'/assets/images/logo.png\',\'header\'); ?>" alt="<?php echo get_bloginfo(\'name\'); ?>" /></a>
                        <?php
                            if(is_home()){
                                echo \'</h1>\';
                            }else{
                                echo \'</h2>\';
                            }


                            $args = apply_filters(\'wplms-main-menu\',array(
                                 \'theme_location\'  => \'main-menu\',
                                 \'container\'       => \'nav\',
                                 \'menu_class\'      => \'menu\',
                                 \'walker\'          => new vibe_walker,
                                 \'fallback_cb\'     => \'vibe_set_menu\'
                             ));

                        ?>


                        <?php
                            if ( function_exists(\'bp_loggedin_user_link\') && is_user_logged_in() ) :
                                ?>
                                <ul class="topmenu">
                                    <li><a href="<?php bp_loggedin_user_link(); ?>" class="smallimg vbplogin"><?php $n=vbp_current_user_notification_count(); echo ((isset($n) && $n)?\'<em></em>\':\'\'); bp_loggedin_user_avatar( \'type=full\' ); ?><span><?php bp_loggedin_user_fullname(); ?></span></a></li>
                                    <?php do_action(\'wplms_header_top_login\'); ?>
                                    <?php
                                    if ( in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) || (function_exists(\'is_plugin_active_for_network\') && is_plugin_active_for_network( \'woocommerce/woocommerce.php\'))) { global $woocommerce;
                                    ?>
                                    <li><a class="smallimg vbpcart"><span class="fa fa-shopping-cart"><?php echo (($woocommerce->cart->cart_contents_count)?\'<em>\'.$woocommerce->cart->cart_contents_count.\'</em>\':\'\'); ?></span></a>
                                    <div class="woocart"><?php woocommerce_mini_cart(); ?></div>
                                    <div id="searchicon"><i class="icon-search-2"></i></div>                            
                                    </li>
                                    <?php
                                    }
                                    ?>
                                </ul>
<?php wp_nav_menu( $args ); ?>
                            <?php
                            else :
                                ?>
                                <ul class="topmenu">
                                    <li><a href="#login" class="smallimg vbplogin"><span><?php _e(\'LOGIN\',\'vibe\'); ?></span></a></li>
                                    <?php
                                    if ( in_array( \'woocommerce/woocommerce.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) || (function_exists(\'is_plugin_active_for_network\') && is_plugin_active_for_network( \'woocommerce/woocommerce.php\'))) { global $woocommerce;
                                    ?>
                                    <li><a class="smallimg vbpcart"><span class="fa fa-shopping-cart"><?php echo (($woocommerce->cart->cart_contents_count)?\'<em>\'.$woocommerce->cart->cart_contents_count.\'</em>\':\'\'); ?></span></a>
                                    <div class="woocart"><?php woocommerce_mini_cart(); ?></div>
                                    <div id="searchicon"><i class="icon-search-2"></i></div>
                                    </li>
                                    <?php
                                    }
                                    ?>
                                </ul>
<?php wp_nav_menu( $args ); ?>    
                            <?php
                            endif;
                        ?>
                        <?php
                            $style = vibe_get_login_style();
                            if(empty($style)){
                                $style=\'default_login\';
                            }
                        ?>
                        <div id="vibe_bp_login" class="<?php echo $style; ?>">
                        <?php
                            vibe_include_template("login/$style.php");
                        ?>
                       </div>
                    </div>
                    <a id="trigger">
                        <span class="lines"></span>
                    </a>
            </div>
        </header>

1 个回复
SO网友:N00b

只要确保JS 位于<script> 标签,您可以使用PHP 任何地方只要.php 文件,它位于<?php?>. 就这么简单!

这有点难看,也远没有超级可读性,但这就是解决问题的方法。

<小时>

<?php
$php_code = \'My PHP is here!!\';

/*Close PHP*/ ?>

<script> //All JS inside the <script> tags

    var jsCode = \'All my JavaScript here!!\';

    <?php
    if( $i_can_also_use_php_here ) { ?>

        var jsAgain = \'Just JS and PHP back and forth!!\'; <?php 

    } ?> // "}" is closing PHP if statement

</script> 

<?php //Open PHP, no more JS, <script> tag is closed

$continue_php = \'Let\'s continue this beast!!\'; ?>

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果