JavaScript:用“or”声明 VAR陈述

发布于 2024-12-05 04:11:33 字数 376 浏览 0 评论 0原文

类似

var $page_products = $page_name == 'products-uninterruptible-power-supply.php' OR 'products-uninterruptible-power-supply-np2031.php';

该代码与我已经尝试使用的

var $page_products = $page_name == 'products-uninterruptible-power-supply.php' || 'products-uninterruptible-power-supply-np2031.php';

,但它仍然无法正常工作。提前致谢!

the code is something like this

var $page_products = $page_name == 'products-uninterruptible-power-supply.php' OR 'products-uninterruptible-power-supply-np2031.php';

i already tried using

var $page_products = $page_name == 'products-uninterruptible-power-supply.php' || 'products-uninterruptible-power-supply-np2031.php';

but it is still not working. thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清晰传感 2024-12-12 04:11:33

假设您的目的是声明一个变量 $page_products,如果 $page_name 等于这两个字符串之一,则该变量将设置为 true,否则设置为 false,请执行以下操作:

var $page_products = $page_name == 'products-uninterruptible-power-supply.php'
           || $page_name == 'products-uninterruptible-power-supply-np2031.php';

编辑:我刚刚注意到两个页面名称都以相同的四个单词开头,因此您不必与每个字符串进行比较,而是可以测试 $page_name 中的值是否以这些单词开头,例如, :

var $page_products =
           $page_name.indexOf('products-uninterruptible-power-supply') === 0;

Assuming your intention is to declare one variable, $page_products, which will be set to true if the $page_name is equal to either of those two strings and false otherwise, do this:

var $page_products = $page_name == 'products-uninterruptible-power-supply.php'
           || $page_name == 'products-uninterruptible-power-supply-np2031.php';

EDIT: I just noticed that both page names start with the same four words, so instead of comparing to each string it may suit you to test whether the value in $page_name starts with those words, e.g.,:

var $page_products =
           $page_name.indexOf('products-uninterruptible-power-supply') === 0;
寻找我们的幸福 2024-12-12 04:11:33

这应该有效

var $page_products = ($page_name == 'products-uninterruptible-power-supply.php' || $page_name == 'products-uninterruptible-power-supply-np2031.php');

This should work

var $page_products = ($page_name == 'products-uninterruptible-power-supply.php' || $page_name == 'products-uninterruptible-power-supply-np2031.php');
许你一世情深 2024-12-12 04:11:33

如果您想有条件地分配文件名,那么您将需要三元运算符:

var $page_products = ($page_name == 'products-uninterruptible-power-supply.php') ?
    'products-uninterruptible-power-supply.php' : 
    'products-uninterruptible-power-supply-np2031.php';

If you want to assign the file name conditionally, then you'll want the ternary operator:

var $page_products = ($page_name == 'products-uninterruptible-power-supply.php') ?
    'products-uninterruptible-power-supply.php' : 
    'products-uninterruptible-power-supply-np2031.php';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文