<script> window.onload = function(){ document.getElementById("but").onclick = function(){ var msg = document.getElementById("inp").value var newObj = document.createElement("p"); newObj.innerHTML = msg document.getElementById("show").appendChild(newObj); document.getElementById("inp").value = ""; } } </script> </head> <body> <div id="show"> <p>你好,中午去哪里吃饭?</p> <p>好久没有去第三食堂了</p> <p>第三食堂有点贵哦</p> </div> <div id="box"> <input id="inp" type="text"> <button id="but">发送信息</button>这段的代码的位置
时间: 2023-10-08 08:06:37 浏览: 70
这段代码应该放在 HTML 页面的 `<head>` 标签中。它定义了一个 `window.onload` 事件,当页面加载完成时,会执行其中的函数。这个函数的作用是监听发送信息按钮的点击事件,当点击按钮时,获取输入框中的值,将其封装在一个新的 `<p>` 标签中,并添加到页面中已有的 `<div>` 标签中。
相关问题
这段代码有什么错误,改正<head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script> window.onload(function() { document.getElementById("bt").onclick(function() { alert("点我干嘛"); }) }) { } </script> </head> <body> <input type="button" value="点我" id="bt" /> </body> </html>
这段代码有以下错误:
1. 缺少<html>标签和<body>标签,需要在代码前面添加<html>标签和<body>标签来包裹代码。
2. window.onload()是一个事件,需要添加事件监听器来监听它,而不是和它一起作为一个函数。
3. document.getElementById()方法后面需要加上括号,表示获取元素后要执行的操作。
4. 代码块 { } 不应该出现在 window.onload() 方法后面。
修改后的代码如下所示:
```
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
window.onload = function() {
document.getElementById("bt").onclick = function() {
alert("点我干嘛");
}
}
</script>
</head>
<body>
<input type="button" value="点我" id="bt" />
</body>
</html>
```
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script type="text/javascript"> window.onload = function(){ var b1 = document.getElementById("big"); var img = document.getElementById("picture"); b1.clickMe() = function(){ img.width = img.width*2; } } </script> <title></title> </head> <body> <img src="../img/定海神针.jpg" width="200px" id="picture"/><br /> <input type="button" value="大大大" onclick="clickMe()" id="big"/> <input type="button" value="小小小" onclick="clickMy()" id="small"/> </body> </html>
您的代码存在一些问题。首先,您的JavaScript代码中的 `clickMe()` 和 `clickMy()` 函数未定义。其次,您在 `window.onload` 函数中没有正确绑定按钮的点击事件。下面是修正后的代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<img src="../img/定海神针.jpg" width="200px" id="picture" /><br />
<input type="button" value="大大大" onclick="zoomIn()" id="big" />
<input type="button" value="小小小" onclick="zoomOut()" id="small" />
<script type="text/javascript">
window.onload = function() {
var b1 = document.getElementById("big");
var b2 = document.getElementById("small");
var img = document.getElementById("picture");
b1.onclick = function() {
img.style.width = (img.offsetWidth * 2) + "px";
};
b2.onclick = function() {
img.style.width = (img.offsetWidth / 2) + "px";
};
};
</script>
</body>
</html>
```
在这个修正后的代码中,我使用了 `img.style.width` 来设置图片的宽度,而不是直接修改 `img.width` 属性。同时,我修正了按钮的点击事件绑定,分别绑定了 `zoomIn()` 和 `zoomOut()` 函数。
现在,当您点击 "大大大" 按钮时,图片的宽度将增加一倍;当您点击 "小小小" 按钮时,图片的宽度将减小一半。