当前位置: 首页 >  浏览器控制台输入的神奇趣味代码有哪些?完整复制粘贴指南

浏览器控制台输入的神奇趣味代码有哪些?完整复制粘贴指南

时间:2026-10-22 来源:捷速平台官网
详情介绍

浏览器控制台输入的神奇趣味代码有哪些?完整复制粘贴指南1

浏览器控制台(console)是开发者在浏览器中查看和操作JavaScript代码的地方。以下是一些可以在控制台中输入的神奇趣味代码:
1. `alert()` 函数:弹出一个包含指定消息的警告框。
2. `prompt()` 函数:弹出一个包含指定消息的对话框,用户可以在其中输入文本。
3. `document.write()` 函数:将指定的字符串写入文档的开头或结尾。
4. `document.body.style.backgroundColor = 'red'`:将整个页面的背景颜色更改为红色。
5. `document.title = "Hello, World!"`:将网页标题更改为 "Hello, World!"。
6. `document.cookie = 'name=John; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT'`:设置一个名为 "name" 的 cookie,其中值为 "John",路径为 "/",过期时间为 1970-01-01 00:00:00 GMT。
7. `document.getElementById('myButton').click()`:点击 id 为 "myButton" 的元素。
8. `document.getElementsByTagName('div')[0].style.color = 'blue'`:将第一个 div 元素的颜色更改为蓝色。
9. `document.querySelectorAll('p').forEach(function(element) { element.style.fontSize = '20px'; });`:将所有 p 元素的字体大小设置为 20px。
10. `document.createElement('div').style.width = '100px'; document.body.appendChild(document.createElement('div'));`:创建一个宽度为 100px 的新 div 元素,并将其添加到 body 中。
11. `window.location.href = 'https://www.example.com';`:跳转到 "https://www.example.com"。
12. `var x = Math.random(); var y = Math.random(); var z = Math.random(); console.log(x + y + z);`:生成三个随机数并输出它们的和。
完整复制粘贴指南:
javascript
// 使用 alert() 函数弹出警告框
alert("这是一个警告框");
// 使用 prompt() 函数弹出对话框
var userInput = prompt("请输入你的名字:");
console.log("用户输入:" + userInput);
// 使用 document.write() 函数将字符串写入文档开头
document.write("

欢迎来到我的网站!

");
// 使用 document.body.style.backgroundColor 将背景颜色更改为红色
document.body.style.backgroundColor = "red";
// 使用 document.title 将网页标题更改为 "Hello, World!"
document.title = "Hello, World!";
// 使用 document.cookie 设置一个名为 "name" 的 cookie
document.cookie = "name=John; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
// 使用 document.getElementById 获取 id 为 "myButton" 的元素并点击它
var button = document.getElementById("myButton");
button.click();
// 使用 document.getElementsByTagName 获取所有 div 元素并修改其颜色
var divs = document.getElementsByTagName("div");
for (var i = 0; i < divs.length; i++) {
divs[i].style.color = "blue";
}
// 使用 document.createElement 创建一个新的 div 元素并设置其宽度和位置
var newDiv = document.createElement("div");
newDiv.style.width = "100px";
document.body.appendChild(newDiv);
// 使用 window.location.href 跳转到 "https://www.example.com"
window.location.href = "https://www.example.com";
// 使用 Math.random() 生成两个随机数并输出它们的和
var x = Math.random();
var y = Math.random();
var z = Math.random();
console.log(x + y + z);
继续阅读
TOP