[英]How to use php code in javascript
我有一個包含以下代碼的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循環。 因此每個循環的行不同。
你可以做的是從.js
文件外部傳遞變量,如下所示:
// index.php
<div class="row">
<div class="small-12 columns">
page content
</div>
</div>
<script>var test = '<?php echo $variable; ?>';</script>
然后引用該變量,就像在.js
文件中定義它一樣(注意潛在的范圍問題)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.