如何在JavaScript中使用PHP代码

时间:2013-12-20 22:16:52

标签: javascript php jquery

我有一个javascript文件grid.js,其中包含以下代码

Preview.prototype = {
        create : function() {
            // create Preview structure:
            this.$title = $( '<h3></h3>' );
            this.$description = $( '<p></p>' );
            this.$href = $('<div class="showbutton"><form id="myform" method="POST" action="#"><div class="linkbtn02"><a href="#">EVOEGEN</a></div></form></div>' );
            this.$details = $( '<div class="og-details"></div>' ).append( this.$title, this.$description, this.$href );
            this.$loading = $( '<div class="og-loading"></div>' );
            this.$fullimage = $( '<div class="og-fullimg"></div>' ).append( this.$loading );
            this.$closePreview = $( '<span class="og-close"></span>' );
            this.$previewInner = $( '<div class="og-expander-inner"></div>' ).append( this.$closePreview, this.$fullimage, this.$details );
            this.$previewEl = $( '<div class="og-expander"></div>' ).append( this.$previewInner );
            // append preview element to the item
            this.$item.append( this.getEl() );
            // set the transitions for the preview and the item
            if( support ) {
                this.setTransition();
            }
        },
}

但我想为this.$href属性使用动态值。

就像这样

this.$href = $('<div class="showbutton"><?php woocommerce_quantity_input(); ?></div>' );

有人可以告诉我如何在php中使用它吗?

请注意,我有一个foreach循环。因此每个循环的行不同。

1 个答案:

答案 0 :(得分:12)

您可以做的是从.js文件外部传递变量,如下所示:

// index.php

<div class="row">
    <div class="small-12 columns">
        page content
    </div>
</div>
<script>var test = '<?php echo $variable; ?>';</script>

然后引用该变量,就像在.js文件中定义它一样(注意潜在的范围问题)。