最重要的是什么?在JavaScript中,它是如何工作的?
JavaScript最重要的是什么?在JavaScript中,它是如何工作的?,javascript,html,Javascript,Html,JavaScriptx.style.backgroundColor=x.style.backgroundColor==“黄色”中的这行代码是什么?“粉色”:“黄色”;“在下面编写的程序中 <!DOCTYPE html> <html> <body> <p>In this example, the setInterval() method executes the setColor() function once
x.style.backgroundColor=x.style.backgroundColor==“黄色”中的这行代码是什么?“粉色”:“黄色”;“
在下面编写的程序中
<!DOCTYPE html>
<html>
<body>
<p>In this example, the setInterval() method executes the setColor() function once every 300 milliseconds, which will toggle between two background colors.</p>
<button onclick="stopColor()">Stop Toggling</button>
<script>
var myVar = setInterval(function(){ setColor() }, 300);
function setColor() {
var x = document.body;
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow";
}
function stopColor() {
clearInterval(myVar);
}
</script>
</body>
</html>
在本例中,setInterval()方法每300毫秒执行一次setColor()函数,该函数将在两种背景色之间切换
停止切换
var myVar=setInterval(函数(){setColor()},300);
函数setColor(){
var x=document.body;
x、 style.backgroundColor=x.style.backgroundColor==“黄色”?“粉色”:“黄色”;
}
函数stopColor(){
净距(myVar);
}
这是最重要的
条件?expr1:expr2
这与:
if (condition) expr1;
else expr2;
因此,在你的情况下:
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"
相当于:
if (x.style.backgroundColor == "yellow")
x.style.backgroundColor = "pink";
else x.style.backgroundColor = "yellow";
这是最重要的
条件?expr1:expr2
这与:
if (condition) expr1;
else expr2;
因此,在你的情况下:
x.style.backgroundColor = x.style.backgroundColor == "yellow" ? "pink" : "yellow"
相当于:
if (x.style.backgroundColor == "yellow")
x.style.backgroundColor = "pink";
else x.style.backgroundColor = "yellow";