2013-12-20 42 views
6

我有containsthe下面的代碼如何使用PHP代碼在JavaScript

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(); 
      } 
     }, 
} 

javascript文件grid.js但我想用動態值this.$href屬性。

它會像這樣

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

有人能告訴我如何使用PHP內?

請注意,我有一個foreach循環。所以這條線對於每個循環都是不同的。

+2

你不能。這是一個js文件,所以你不能有PHP。除非你改變你的文件擴展名爲'.php'或者改變服務器處理JS文件的方式。 – putvande

+0

我知道我不能在外部js文件中使用php,這就是爲什麼我問這個問題。我的意思是我想在我的php文件中使用那一行 – Giri

+0

你需要讓你的php輸出數據的方式允許javascript來循環它。像你所問的那樣跨越客戶端和服務器代碼是不可能的。 – Entoarox

回答

11

你可以做的是從.js文件外傳遞變量,就像這樣:

// index.php 

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

,然後引用變量一樣,如果你.js文件中定義它(介意的潛在範圍問題)。