Javascript 如果……如果……如果……不起作用
这就是它的工作原理:从 问题: x==0 | | 1到x==0 | | x==1 等等 这是从.html运行的Javascript 如果……如果……如果……不起作用,javascript,html,css,logical-operators,Javascript,Html,Css,Logical Operators,这就是它的工作原理:从 问题: x==0 | | 1到x==0 | | x==1 等等 这是从.html运行的 <body> <div class="" id="pimg1"> </body><script> var x=new Date().getMonth(); if (x == 0||x == 5||x == 2){document.getElementById('pimg1').classNam
<body>
<div class="" id="pimg1">
</body><script>
var x=new Date().getMonth();
if (x == 0||x == 5||x == 2){document.getElementById('pimg1').className='pimg1';}
else
if (x == 3||x == 4){document.getElementById('pimg1').className="pimg1a";}
else
if (x == 6||x == 7||x == 8){document.getElementById('pimg1').className='pimg1b';}
else {document.getElementById('pimg1').className='pimg1c';}
</script></html>
首先,将javascript代码放在标记之间,因为javascript代码不会在html标记中运行 然后,使用x==0 | | x==9 | x==2,而不是x==0 | | 9 | 2 请缩进您的代码以便于阅读。| |运算符在let和右侧查找条件。9不是条件,2也不是条件。当你把x==9 | | x==2,你说的是检查左边的条件,如果不是真的,检查右边的条件。如果一个是真的,那么我们就可以开始了。我们不能在标记中使用css,也可以从标记中删除标记,如下所示:
<html>
<head>
<style>
.pimg1 {
background-image: url('images/style1.jpg');
/*Zone 1*/
}
.pimg1a {
background-image: url('images/style2.jpg');
/*Zone 1*/
}
.pimg1b {
background-image: url('images/style3.jpg');
/*Zone 1*/
}
.pimg1c {
background-image: url('images/style4.jpg');
/*Zone 1*/
}
</style>
</head>
<body>
<div class="" id="pimg1"></div>
<script>
var x = new Date().getMonth();
if (x == 0 || 9 || 2) {
document.getElementById('pimg1').className = 'pimg1';
} else if (x == 3 || 5) {
document.getElementById('pimg1').className = "pimg1a";
} else if (x == 6 || 1 || 8) {
document.getElementById('pimg1').className = 'pimg1b';
} else {
document.getElementById('pimg1').className = 'pimg1c';
}
</script>
</body>
</html>
这是什么语言?x==0 | | 9 | | 2=>x==0 | | x==9 | | x==2这是javascript,不是直接放在html文件中的javascript块中,这意味着它被认为是文本,而不是代码投票关闭,因为一个无法再复制的问题或一个简单的印刷错误。这很漂亮。你知道如何编写一个基本的if条件吗?
<html>
<head>
<style>
.pimg1 {
background-image: url('images/style1.jpg');
/*Zone 1*/
}
.pimg1a {
background-image: url('images/style2.jpg');
/*Zone 1*/
}
.pimg1b {
background-image: url('images/style3.jpg');
/*Zone 1*/
}
.pimg1c {
background-image: url('images/style4.jpg');
/*Zone 1*/
}
</style>
</head>
<body>
<div class="" id="pimg1"></div>
<script>
var x = new Date().getMonth();
if (x == 0 || 9 || 2) {
document.getElementById('pimg1').className = 'pimg1';
} else if (x == 3 || 5) {
document.getElementById('pimg1').className = "pimg1a";
} else if (x == 6 || 1 || 8) {
document.getElementById('pimg1').className = 'pimg1b';
} else {
document.getElementById('pimg1').className = 'pimg1c';
}
</script>
</body>
</html>