简体   繁体   English

为什么我的JavaScript出错?

[英]Why am I getting errors with my JavaScript?

I am trying to practice JavaScript with basic DOM manipulation but I keep getting 2 errors: 我正在尝试通过基本的DOM操作来练习JavaScript,但我不断遇到2个错误:

1.SyntaxError: missing ; 1.SyntaxError:缺少; before statement 声明前

2.TypeError: size is not a function 2.TypeError:大小不是函数

 function size() { var height = document.body.getElementById('hover1').style.height: 500 px; var width = document.body.getElementById('hover1').style.width: 500 px; } console.log(hover); 
 <!DOCTYPE html> <html> <head> <title>javascript practice</title> <meta charset="utf-8" /> <style> body { margin: 7em auto; background-color: green; width: 90%; } #size1 { background-color: blue; width: 150px; height: 150px; margin: 3em auto; } </style> </head> <body> <div id="size1"></div> <input type="button" value="size" onclick="size();"> </body> 

your error is here: 您的错误在这里:

var height = document.body.getElementById('hover1').style.height: 500px;
var width = document.body.getElementById('hover1').style.width: 500px;
// -------------------^  and -------------------------------------^

replace those lines with these: 将这些行替换为:

var height = document.getElementById('hover1').style.height;
var width = document.getElementById('hover1').style.width;

There is nothing called document.body.getElementById instead change this to document.getElementById use as below 没有什么叫做document.body.getElementById而是将其更改为document.getElementById ,如下所示

var height = (document.getElementById('hover1'))?document.getElementById('hover1').style.height: "500px";
var width = (document.getElementById('hover1'))?document.getElementById('hover1').style.width: "500px";

 function size1() { console.log("test") var height = (document.getElementById('hover1'))?document.getElementById('hover1').style.height: "500px"; var width = (document.getElementById('hover1'))?document.getElementById('hover1').style.width: "500px"; } //console.log(hover); 
 <!DOCTYPE html> <html> <head> <title>javascript practice</title> <meta charset="utf-8"/> <style> body { margin: 7em auto; background-color: green; width: 90%; } #size1 { background-color: blue; width: 150px; height: 150px; margin: 3em auto; } </style> </head> <body> <div id="size1"> </div> <input type="button" value="size" onclick="size1()"> </body> 

If you are going to set the height of the element and assign the height to var height then you need to do the following: 如果要设置元素的高度并将高度分配给var height则需要执行以下操作:

var height = document.getElementById("size1").style.height = "500px";

If you are going to just assign the height of the element to var height then you need to do the following: 如果要仅将元素的高度分配给var height则需要执行以下操作:

var height = document.getElementById("size1").style.height;

If you are just going to set the height of the element then you just need to do: 如果您只是要设置元素的高度,则只需执行以下操作:

document.getElementById("size1").style.height = "500px";

You should also make your 500px with quotes as numbers do not need " " around them but once you add text...it all becomes text. 您还应该将500px用引号引起来,因为数字不需要在它们500px加上" " ,但是一旦添加了文本,所有内容都将变成文本。 So either way you should have "500px" 因此,无论哪种方式,您都应该具有"500px"

I would also make your button into this: 我也将您的按钮设置为:

<button type="button" value="size" onclick="size()">Size</button>

Based on a belief that you are trying to make the div larger onclick of Size button then you should have the following: 基于一种信念,即您试图通过“ Size按钮的onclick来使div更大,那么您应该具有以下几点:

Style 样式

body {
    margin: 7em auto;
    background-color: green;
    width: 90%;
}

#size1 {
    background-color: blue;
    width: 150px;
    height: 150px;
    margin: 3em auto;
}

HTML HTML

<div id="size1"></div>

<button type="button" value="size" onclick="size()">Size</button>

JavaScript 的JavaScript

function size() {
  document.getElementById('size1').style.height = "500px";
  document.getElementById('size1').style.width = "500px";
}

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

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