简体   繁体   English

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

[英]Why doesn't my if-else statement work?

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)
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您必须检查每个 url 如果它是字符串使用引号

I don't think you got your if condition right:我认为您的 if 条件不正确:

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". "url==showbasket.html" 检查 "url" 是否等于 object "showbasket" 的 "html" 属性。 Since showbasket does not exist, your code will throw an exception.由于 showbasket 不存在,您的代码将引发异常。

"||order1.html" means the same, check if the "html" attribute of "order1" object is "true" "||order1.html" 含义相同,检查"order1" object 的"html" 属性是否为"true"

Like others have said, what you want to do is:就像其他人说的那样,你想做的是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM