我在代码中一个接一个地放了两个if else语句。当我添加第二个时,代码根本不起作用。我收到一个语法错误,表示期待分号。怎么了?
这是使用if if的工作代码。
window.onload = function() {
var div = document.createElement("div");
div.innerHTML = '<img alt="image" src="images/image.png">';
div.style.visibility = "visible";
div.style.position = "absolute";
div.style.zIndex = "3";
div.className = "div";
div.id = "div";
document.body.appendChild(div);
document.onmousemove=function(e){
var x, y;
var div = document.getElementById('div');
if (e) {x=e.clientX; y=e.clientY;} else {x=event.clientX;y=event.clientY;};
div.style.top=y+'px';
div.style.left=x-90+'px';
}};
&#13;
<html>
<body>
</body>
</html>
&#13;
这是一个有两个if else语句无效的
window.onload = function() {
var div = document.createElement("div");
div.innerHTML = '<img alt="image" src="images/image.png">';
div.style.visibility = "visible";
div.style.position = "absolute";
div.style.zIndex = "3";
div.className = "div";
div.id = "div";
document.body.appendChild(div);
document.onmousemove=function(e){
var x, y;
var div = document.getElementById('div');
if (e) {x=e.clientX; y=e.clientY;} else {x=event.clientX;y=event.clientY;};
if (x = 0) {div.style.visibility=hidden;} else {div.style.visibility= visible;};
div.style.top=y+'px';
div.style.left=x-90+'px';
}};
&#13;
<html>
<body>
</body>
</html>
&#13;
提前谢谢!
答案 0 :(得分:1)
if (x = 0) {div.style.visibility: hidden;} else {div.style.visibility: visible;};
这一行并不代表您认为的含义。您将0
分配给x
,而不是将0
与x
进行比较,冒号是语法错误。此外,该行末尾还有一个空语句。