将PHP变量用于Jquery

时间:2014-01-16 13:43:10

标签: javascript php jquery

您好我正在使用http://brutaldesign.github.io/swipebox/在我的页面中显示图像,我需要一种方法将变量从php传递到Jquery 我正在修改的swipebox代码是

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 我该怎么办?

1 个答案:

答案 0 :(得分:4)

这是因为PHP解释器不处理您的JavaScript文件。

您的问题有不同的解决方案。例如,您可以渲染

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

在PHP呈现代码中,然后在JS脚本中使用artId变量。