[英]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.