【问题标题】:Including HTML/PHP in JQuery在 JQuery 中包含 HTML/PHP
【发布时间】:2014-04-21 20:50:47
【问题描述】:

我正在使用 PHP HTML CSS 和 JQuery 的组合为一家制造公司开发一个网站。我希望用户能够单击“联系我们”按钮以打开一个模式窗口,其中包含一个销售代表表格,他们可以联系该表格以获取报价和其他此类联系信息。我已经编写了一个 PHP 文件,它将生成和格式化我有兴趣显示的所有信息,有没有办法可以将它包含到 JQuery .ModalWindow() 调用中?

jQuery:

<script type="text/javascript">
$(document).ready(function(){
$('.modalLink').ModalWindow();
$('#ContactUs').ModalWindow({
height: '750', width: '750',title: 'Does This Work?',description: 'I Want To Include the PHP here'});});
</script>

模态窗口“类”:

(function($){
    // Defining our jQuery plugin

$.fn.ModalWindow = function(prop){

    // Default parameters

    var options = $.extend({
        height : "250",
        width : "500",
        title:"Hey James isn't this slick?",
        description: "This is where we put the stuff that does the things.",
    },prop);

    return this.click(function(e){
        add_block_page();
        add_popup_box();
        add_styles();

        $('.ModalWindow').fadeIn();
    });

     function add_styles(){ 
                    /*Block page overlay*/
        var pageHeight = $(document).height();
        var pageWidth = $(window).width();

        $('.ModalWindow').css({ 
            'position':'absolute', 
            'top':'50%',
            'left':'50%',
            'margin':'-125px auto auto -250px',             
            'display':'none',
            'height': options.height + 'px',
            'width': options.width + 'px',
            'border':'1px solid #fff',
            'box-shadow': '0px 2px 7px #292929',
            '-moz-box-shadow': '0px 2px 7px #292929',
            '-webkit-box-shadow': '0px 2px 7px #292929',
            'border-radius':'10px',
            '-moz-border-radius':'10px',
            '-webkit-border-radius':'10px',
            'background': '#f2f2f2', 
            'z-index':'50',
        });
        $('.ModalClose').css({
            'position':'relative',
            'top':'0px',
            'left':'0px',
            'float':'right',
            'display':'block',
            'height':'30px',
            'width':'30px',
            'background': 'url(/PictureLibrary/ModalWindowImages/CloseButton.png) no-repeat',
        });

        $('.BlockPage').css({
            'position':'absolute',
            'top':'0',
            'left':'0',
            'background-color':'rgba(0,0,0,0.6)',
            'height':pageHeight,
            'width':pageWidth,
            'z-index':'10'
        });
        $('.InnerModalWindow').css({
            'background-color':'#fff',
            'height':(options.height - 50) + 'px',
            'width':(options.width - 50) + 'px',
            'padding':'10px',
            'margin':'15px',
            'border-radius':'10px',
            '-moz-border-radius':'10px',
            '-webkit-border-radius':'10px'
        });
    }

     function add_block_page(){
        var block_page = $('<div class="BlockPage"></div>');

        $(block_page).appendTo('body');
    }

     function add_popup_box(){
         var pop_up = $('<div class="ModalWindow"><a href="#" class="ModalClose"></a><div class="InnerModalWindow"><h2>' + options.title + '</h2><p>' + options.description + '</p></div></div>');
         $(pop_up).appendTo('.BlockPage');

         $('.ModalClose').click(function(){
            $(this).parent().fadeOut().remove();
            $('.BlockPage').fadeOut().remove();              
         });
    }

    return this;
};

 })(jQuery);

【问题讨论】:

  • Ken Wheeler 的answer 是正确的。为了清楚起见:您不能在客户端 JavaScript/jQuery 模式中“包含”PHP,但您可以告诉 jQuery 向服务器发送请求以执行脚本,然后在模态的。
  • 谢谢,虽然我不太确定 Ken 的回答中发生了什么,并且正在寻找一些详细说明。

标签: php jquery html include


【解决方案1】:

添加一个 contentURL 选项,然后使用 $.ajax 抓取内容并使用 $('.ModalWindow').html(result) 填充窗口。

【讨论】:

  • 我不太确定我明白你在说什么。你能详细说明一下吗?
  • 如果您想将 php 页面加载到 javascript 模态中,假设您的页面位于 mysite.com/popup.php。您需要向该 URL 发出 ajax 请求。当请求返回时,在成功方法中,将模式窗口的内容设置为响应。 api.jquery.com/jQuery.ajax
【解决方案2】:
<script type="text/javascript">
(document).ready(function(){
$('.modalLink').ModalWindow();
$('#ContactUs').ModalWindow({
height: '750', width: '750',title: 'Does This Work?',description: <?php echo $description; ?>});});
</script>

不确定这是否适合你,因为这个答案很简单,但是: 如果您有生成和格式化所有将显示的信息的 php 函数。只需将该信息以字符串格式输入,您就可以直接将其回显。这显然是因为 php 是服务器端,而 html 和 JQuery 是前端。

例如: $description='&lt;div id="container"&gt;&lt;p&gt;hello world&lt;p&gt;&lt;a href="http://google.com"&gt;google link&lt;/a&gt;&lt;/div&gt;';

【讨论】:

  • 您能否详细说明一下,以便 OP 了解您正在使用此代码做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 2021-03-02
相关资源
最近更新 更多