如何在JavaScript中使用PHP代码

Gir*_*iri 7 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();
            }
        },
}
Run Code Online (Sandbox Code Playgroud)

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

会是这样的

this.$href = $('<div class="showbutton"><?php woocommerce_quantity_input(); ?></div>' );
Run Code Online (Sandbox Code Playgroud)

有人能告诉我如何使用PHP内部?

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

Zac*_*ach 12

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

// index.php

<div class="row">
    <div class="small-12 columns">
        page content
    </div>
</div>
<script>var test = '<?php echo $variable; ?>';</script>
Run Code Online (Sandbox Code Playgroud)

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