为什么我的 if-else 语句不起作用?
任何人都可以明白为什么这不起作用:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须检查每个网址,如果它是字符串,请使用引号
You have to do the check for each url and if it's a string use quotes
我认为你的 if 条件不正确:
I don't think you got your if condition right:
此代码是有效的,但它不会执行您想要的操作
“url==showbasket.html”检查“url”是否等于对象“showbasket”的“html”属性。由于 showbasket 不存在,您的代码将引发异常。
“||order1.html”的意思相同,检查“order1”对象的“html”属性是否为“true”
就像其他人所说的那样,你想做的是:
This code is valid, but it will not do what you want
"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 :