2014-01-16 56 views
0

嗨,我使用http://brutaldesign.github.io/swipebox/在我的頁面來顯示圖像,我需要一種方法來從PHP變量傳遞給jQuery的,我正在修改 swipebox代碼使用PHP變量到jQuery的

plugin = this, 
    elements = [], // slides array [{href:'...', title:'...'}, ...], 
    elem = elem, 
    selector = elem.selector, 
    $selector = $(selector), 
    isTouch = document.createTouch !== undefined || ('ontouchstart' in window) || ('onmsgesturechange' in window) || navigator.msMaxTouchPoints, 
    supportSVG = !!(window.SVGSVGElement), 
    winWidth = window.innerWidth ? window.innerWidth : $(window).width(), 
    winHeight = window.innerHeight ? window.innerHeight : $(window).height(), 
    html = '<div id="swipebox-overlay">\ 
      <div id="swipebox-slider"></div>\ 
      <div id="swipebox-caption"><a id="swipebox-close"></a></div>\ 
      <a id="swipebox-prev"></a>\ 
      <a id="swipebox-next"></a>\ 
      <div id="swipebox-action"><span class="swipebox-caption-text"></span>\ 
      <div class="listing-details-toolbar-inner">\ 
      <ul class="action-link-group float-l">\ 
      <li class="action-link-item">\ 
      <a href="#" id="shareListingBtn"\ 
             data-artid="<?php print $artId; ?>"\ 
             class="link-btn icon-btn ui-button-small"><span class="ic-icon share-icon"></span>Share</a>\ 
      </li>\ 
      <li class="action-link-item">\ 
      <a id="addToCollection"\ 
       data-artid="<?php print $artId;?>"\ 
       data-show_collections="<?php print TRUE; ?>"\ 
       data-title="<?php print $listing_single->title;?>"\ 
       data-description="<?php print $listing_single->description; ?>"\ 
       class="addto-Collection-btn link-btn ui-button-xsmall ui-button-primary">Collect it\ 
      </a>\ 
     </li>\ 
      <li class="action-link-item">\ 
      <?php print $smallLikeButton; ?>\ 
     </li>\ 
     </ul>\ 
     </div>\ 
    </div>'; 

正如你所看到的HTML是從原來的

html = '<div id="swipebox-overlay">\ 
      <div id="swipebox-slider"></div>\ 
      <div id="swipebox-caption"><a id="swipebox-close"></a></div>\ 
      <a id="swipebox-prev"></a>\ 
      <a id="swipebox-next"></a>\ 
      <div id="swipebox-action"><span class="swipebox-caption-text"></span></div>\ 
    </div>'; 

不同我的問題是我添加按鈕插件HTML,我需要使用變量,像這樣的

<?php print $artId; ?> 

enter image description here 我該怎麼辦?

回答

4

這是因爲你的JavaScript文件沒有被PHP解釋器處理。

針對您的問題有不同的解決方案。例如,您可以在您的PHP渲染代碼中渲染

<script> 
    var artId = <?php echo $artId; ?>; // assuming $artId is number 
</script> 

然後在JS腳本中使用artId變量。