let obj = {
name: "小明",
age: 18,
height: "1.70m",
//定义方法
obj: function() {
document.write(this.name);
document.write(this.age);
}
}
let obj = {
name:"小明",
}
obj.age=18
let obj = {
name:"小明",
}
delete obj.name
for (let k in obj) {
document.write(k + ":");
document.write(obj[k]);
document.write("
");
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>123</div>
<ul>
<li>测试1</li>
<li>测试2</li>
<li>测试3</li>
</ul>
<script>
const dom = document.querySelector('div');
console.log(dom)
const li = document.querySelector("ul li:first-child");
console.log(li)
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=, initial-scale=1.0" >
<title>Document</title>
</head>
<body>
<div class="box">我是文本内容</div>
<script>
//1.获取元素
const dom = document.querySelector(".box");
//2.修改元素文字内容
dom.innerText = "我是一个盒子";
//3解析元素
dom.innerHTML = "修改标签"
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>修改元素属性</title>
</head>
<body>
<img src="1.jpg" alt="">
<script>
// 1.获取标签元素
let img=document.querySelector("img");
// 2.修改图片对象属性,对象.属性 = 赋值
img.src="2.jpg";
</script>
</body>
</html>
注意:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div{
width:200px;
height:300px;
background-color: bisque;
}
</style>
</head>
<body>
<div class="box"></div>
<script>
// 1.获取元素
let div = document.querySelector('.box');
// 2.修改元素样式属性
div.style.width = '500px';
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width:500px;
height:600px;
background-color: beige;
border:1px solid pink;
color:red
}
</style>
</head>
<body>
<div class="box">111111111111</div>
<script>
// 1.获取元素
let div = document.querySelector('div');
// 2.添加类名
div.className="box";
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box{
width:300px;
height:200px;
color:red;
}
.active{
color:powderblue;
background-color: pink;
font-size: large;
}
.delete{
width:20px;
height:30px;
}
</style>
</head>
<body>
<div class="box">文字</div>
<script>
// 1.获取元素
let div = document.querySelector("div");
// 2.添加类
div.classList.add("active");
div.classList.add("delete");
//3.删除类
div.classList.remove("delete");
// 4.切换类,如果目前有就删掉,没有就添加上
div.classList.toggle("active");
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="text" value="请输入">
<script>
//1.获取元素
const uname=document.querySelector("input")
// 2.获取值
console.log(uname.value);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box" data-id="10">盒子标签</div>
<script>
//获取元素
const box=document.querySelector(".box");
//调用
console.log(box.dataset.id);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function out(){
document.write("定时器触发");
document.write("
");
}
setInterval(out,1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="btn" disabled>倒计时(10)</button>
<script>
let i = 10;
let btn = document.querySelector(".btn");
function time(){
i--;
btn.innerHTML="倒计时(" + i + ")";
if(i==0){
clearInterval(n);
btn.disabled=false;
btn.innerHTML="倒计时结束";
}
}
let n = setInterval(time,1000);
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button class="btn">按钮</button>
<script>
const btn = document.querySelector(".btn");
btn.addEventListener("click",function(){
alert("按钮被点击了")
})
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="dad">
<div class="son">子节点</div>
</div>
<script>
const son = document.querySelector(".son");
console.log(son.parentNode);//获取父节点
</script>
</body>
</html>