簡體   English   中英

JS if 語句中的 PHP else if 語句條件為真時出錯。

[英]Error when PHP else if statement condition is true inside the JS if statement.

我在我的函數中結合了一些 jQuery 和 PHP。 谷歌瀏覽器正確呈現代碼,結果如預期,但是,Opera 或 Firefox Uncaught SyntaxError: missing ) after argument list錯誤Uncaught SyntaxError: missing ) after argument list給我Uncaught SyntaxError: missing ) after argument list

PHP 部分只是檢查客戶是否登錄到我的Magento商店,然后添加正確的 div。

PHP/JS:

AddWelcomeBar();

jQuery(window).resize(function() {
        AddWelcomeBar()
});

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerName = $customerSession->getCustomer()->getName();
?>

function AddWelcomeBar() {
    var innerWidth = window.innerWidth;
    if (innerWidth < 850) {
        <?php   if ($customerSession->isLoggedIn()) { ?>
            var customer = "<?php echo $customerName ?>";
            var welcomebar = jQuery(".welcomebar");
            if (welcomebar.length == 0) {
                jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>Welcome," + customer + "</div>");
            <?php } else { ?>
                jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>10% OFF YOUR 1ST ORDER: CODE 'VV10'</div>");
            <?php } ?>
    } else if (innerWidth > 850) {
            jQuery(".section-item-content > .welcomebar").remove();
            jQuery(".section-item-content").show();
    }
  }
}

HTML:

<div class="section-item-content">

</div>

如果我刪除最后一個關閉} ,那么它適用於 Opera 和 Firefox,但谷歌瀏覽器說缺少} 有任何想法嗎?

基本上,在一個瀏覽器上修復問題,在另一個瀏覽器上破壞代碼。

知道了!

PHP if else在運行時關閉if (welcomebar.length == 0) {當它不應該這樣做時。 第一個條件有效,但是,php else弄亂了 JS if 語句。

if(welcomebar.length == 0) {包裝 PHP if 語句解決了我的問題。

這是工作代碼:

function AddWelcomeBar() {
            var innerWidth = window.innerWidth;
            if (innerWidth < 850) {
                var welcomebar = jQuery(".welcomebar");
                if (welcomebar.length == 0) {
                    <?php   if ($customerSession->isLoggedIn()) { ?>
                        var customer = "<?php echo $customerName ?>";
                            jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>Welcome," + customer + "</div>");
                        <?php } else { ?>
                            jQuery(".section-item-content").prepend("<div class='welcome-message-mobile welcomebar'>10% OFF YOUR 1ST ORDER: CODE 'VV10'</div>");
                        <?php } ?>
                    }
            } else if (innerWidth > 850) {
                    jQuery(".section-item-content > .welcomebar").remove();
                    jQuery(".section-item-content").show();
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM