javascript - 如何在javascript中添加php

标签 javascript php jquery wordpress

需要有关此脚本的一些帮助

(function($) {
    var oldHide = $.fn.popover.Constructor.prototype.hide;

    $.fn.popover.Constructor.prototype.hide = function() {
        if (this.options.trigger === "hover" && this.tip().is(":hover")) {
            var that = this;
            // try again after what would have been the delay
            setTimeout(function() {
                return that.hide.call(that, arguments);
            }, that.options.delay.hide);
            return;
        }
        oldHide.call(this, arguments);
    };
})(jQuery);

$(document).ready(function () {
    $('#example').popover({
        html: true,
        trigger: 'hover',
        container: '#example',
        placement: 'bottom',
        content: function () {
            return '<div class="box">here is some content</div>';
        },
        animation: false,
        delay: {
            hide: 1500
        }
    }).on({
        show: function () {
            console.log("shown");
        },
        hide: function () {
            console.log("hidden");
        }
    });
});

这是一个引导弹出窗口,我想要的只是在其中添加一个 php 脚本

<div class=box><?php php_here(); ?></div>

因为我需要输出一个wordpress脚本

<?php wp_nav_menu(); ?>

这可能吗?

最佳答案

只要您尝试写入 php 的文件是 .php 文件,或者服务器像处理该文件一样处理它,您就可以在其中写入 php。
如果这是 .js 文件,您可能必须将其设为 .php 文件,服务器才能正确处理它。但除此之外,它应该按照您想要的方式工作(有一些方法可以让服务器将 javascript 文件处理为 php 文件,但这可能不是最好的主意,而且它比仅仅重命名 js 文件要多一些工作)。
如果将其更改为 .php 文件,您将需要向客户端发送类型为 application/javascript内容类型 header 才能正确处理。

示例:

test.php

<?php header('Content-Type: application/javascript'); ?>
alert('This is a .php file with javascript in it!');

index.html

<script src="test.php"></script>
//Should show an alert with "This is a .php file with javascript in it!" when opening index.html 

编辑:添加示例

关于javascript - 如何在javascript中添加php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20727611/

相关文章:

javascript - jQuery 可以在 jsFiddle 中使用,但不能在 Drupal 中使用

javascript - 使用 Vue.JS 中的计算属性 v-bind 图像 src

php - 创建模块 prestashop 1.5

php - Mysql重启后恢复Memory表数据

javascript - 在 javascript 字符串中写入 runat ="server"

php - 在 PHP 中将包含空字节的字符串(PNG 图像)写入 MySQL

使用 jsonp 来自 Jquery 的 REST WCF url 中的 javascript 错误

jquery - 显示以前隐藏的 Bootstrap 警报

javascript - jquery.kyco.googleplusfeed.min.js 未缩小的源代码