DOM工作原理:
硬盘代码读取到内存 -> 浏览器渲染引擎生成DOM树(document) -> 开始渲染页面
js为什么可以操作网页的html元素?
因为浏览器会将html代码读取到内存中,生成一颗dom树对象(document),修改了dom树内容,最终页面也会变化
1. document.querySelector('css选择器')
a. 只能获取满足选择器条件的 第一个元素
b. 如果无法获取元素, 则会返回null
2. document.querySelectorAll('css选择器')
a. 可以获取满足选择器条件的 所有元素
b. 返回值一定是数组(伪数组)
<div id="box1" class="one">div1</div>
<div id="box2" class="one">div2</div>
<p class="one">ppppp</p>
<script>
var one = document.querySelector(".one");
console.log(one);//<div id="box" class="one">div1</div>
var box = document.querySelector("#box2");
console.log(box);//<div id="box2" class="one">div2</div>
var oneList = document.querySelectorAll(".one");
console.log(oneList);//NodeList(3) [div#box1.one, div#box2.one, p.one]
var two = document.querySelector(".two");
console.log(two);//如果没有该对象则返回null
var box3 = document.querySelectorAll(".two");
console.log(box3);//返回一个空的NodeList []
//伪数组:有数组三要素,没有数组的api
// oneList.reverse();//程序报错
</script>
元素.属性名
(其实就是对象的取值赋值语法)元素.属性名 = 属性值
div.style.height
得到150px 得到的是一个带单位的字符串-
background-color
,margin-top
,使用js获取和设置这些属性的时候需要使用驼峰命名(因为-符号不符合js的命名规范)div.style.backgroundColor
div.className += " two";//字符串拼接添加类型,注意多个类名之间的空格
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box1{
width: 200px;
height: 200px;
border: 1px solid rgb(0, 0, 0);
font-size: 30px;
font-weight: 700;
}
</style>
</head>
<body>
<div id="box1" class="box" style="
color:red;
background-color: burlywood;
margin-left:100px;
">
hello Javascript
</div>
<script>
var box1 = document.querySelector("#box1");//获取元素
//2.操作元素的属性 :对象的取值/赋值语法 (点语法 或 字符串语法)
//2.1 取值: 对象名.属性名
console.log(box1.id);//box1
console.log(box1['id']);//box1
console.log(box1.style);//css样式属性对象
console.log(box1.style.color);//red
console.log(box1.style.marginLeft);//100px 注意:marginLeft用驼峰命名法才能取到值
//只能获取到行内样式的属性值,无法得到行外(内联/外联)样式的属性值
console.log(box1.style.height);//因为没有定义在行内样式 返回空字符串
//2.2 赋值: 对象名.属性名 = 值
/*
注意点:html属性带- ,background- font- margin- padding-,在js中都需要转成驼峰命名法
a.驼峰命名法: (1)去掉- (2)将-后面的首字母变成大写
b.原因: - 不符合js命名规则
*/
box1.style.width="300px";
box1.style.fontSize = "16px";
box1.style.paddingTop = "30px";
</script>
</body>
</html>
事件源.事件类型 = 事件处理函数
box.onclick = function(){}
任何元素
都可以
注册 很多个事件(点击,移入等)
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#box1{
width: 100px;
height: 100px;
border: 5px solid pink;
background-color: burlywood;
font-size: 16px;
}
</style>
</head>
<body>
<div id="box1">点我触发事件</div>
<script>
var box1 = document.querySelector("#box1");
box1.onclick = function(){
console.log("你点击了我");
}
box1.onmouseover = function(){
console.log("你触发了鼠标移入事件");
box1.style.backgroundColor = "red";
}
box1.onmouseout = function(){
console.log("你触发了鼠标移出事件");
box1.style.backgroundColor = "yellow";
}
box1.ondblclick = function(){
console.log("你触发了双击事件");
box1.style.fontSize = "30px";
box1.style.width = "200px";
box1.style.height = "200px";
}
</script>
</body>
</html>
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。