微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在JavaScript中使用PHP代码

我有一个包含以下代码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();
            }
        },}

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

它会是这样的

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

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

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

解决方法

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

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

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

原文地址:https://www.jb51.cc/js/158437.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐