为什么我的 if-else 语句不起作用?

发布于 2024-11-17 12:32:37 字数 550 浏览 2 评论 0原文

任何人都可以明白为什么这不起作用:

<script>
  if (url==showbasket.html||order1.html||order2.html||order3.html||order4.html) {
     document.write('<span style="font-family:lucida;font-size:10px;">Hello</span>');
  } else {
     document.write('<span style="font-family:lucida;font-size:30px;">Hello Hello</span>');
  }
</script>

我正在尝试编写一个脚本来执行此操作:

IF URL = 1.html or 2.html or 3.html or 4.html THEN 
    write option1 
ELSE 
    write option2 (for all other URL´s)

Can anyone see why this does not work:

<script>
  if (url==showbasket.html||order1.html||order2.html||order3.html||order4.html) {
     document.write('<span style="font-family:lucida;font-size:10px;">Hello</span>');
  } else {
     document.write('<span style="font-family:lucida;font-size:30px;">Hello Hello</span>');
  }
</script>

I´m trying to write a script that do this:

IF URL = 1.html or 2.html or 3.html or 4.html THEN 
    write option1 
ELSE 
    write option2 (for all other URL´s)

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

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

发布评论

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

评论(3

三五鸿雁 2024-11-24 12:32:37
if (url == "showbasket.html" || url == "order1.html" || url == "order2.html" || url == "order3.html" || url == "order4.html")

您必须检查每个网址,如果它是字符串,请使用引号

if (url == "showbasket.html" || url == "order1.html" || url == "order2.html" || url == "order3.html" || url == "order4.html")

You have to do the check for each url and if it's a string use quotes

七七 2024-11-24 12:32:37

我认为你的 if 条件不正确:

if (url == showbasket.html || url == order1.html || ...

I don't think you got your if condition right:

if (url == showbasket.html || url == order1.html || ...
揽月 2024-11-24 12:32:37

此代码是有效的,但它不会执行您想要的操作

if (url==showbasket.html||order1.html

“url==showbasket.html”检查“url”是否等于对象“showbasket”的“html”属性。由于 showbasket 不存在,您的代码将引发异常。

“||order1.html”的意思相同,检查“order1”对象的“html”属性是否为“true”

就像其他人所说的那样,你想做的是:

if ( url == "showbasket.html" || url == "order1.html"

This code is valid, but it will not do what you want

if (url==showbasket.html||order1.html

"url==showbasket.html" checks if "url" is equal to the "html" attribute of object "showbasket". Since showbasket does not exist, your code will throw an exception.

"||order1.html" means the same, check if the "html" attribute of "order1" object is "true"

Like others have said, what you want to do is :

if ( url == "showbasket.html" || url == "order1.html"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文