目录
1、HTML的块级元素,行内元素,行内块元素分别有哪些,区别又是什么
2、textContent、innerText、innnerHTML的区别
1、HTML的块级元素,行内元素,行内块元素分别有哪些,区别又是什么
块级元素:div,h1~h6,p,ul,ol,dl,li,hr,dt,dd,form,table
块元素独占一行
宽高生效,默认宽和父元素一样,内容撑开高度
margin,padding全部生效
行内元素:em,i,del,samll,strong,ins,span,a
宽高不生效,左右margin生效上下不生效,在一行排列,大小靠内容撑开,padding生效
行内块元素:img,input(表单元素,除去form)
在一行排列,宽高生效,margin,padding生效
Position几个定位
relavtive: 相对定位,不脱离文档流,相对自身原本位置进行偏离,不影响元素本身属性,z-index提升层级
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.one{
margin-left: 200px;
width: 300px;
height: 300px;
background-color: red;
}
.two{
width: 100px;
height: 100px;
position: relative;
left: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div class="one">
<div class="two">
</div>
</div>
</body>
</html>
absolute:绝对定位,脱离文档流,相对已有定位的父元素进行偏离,如果没有就相当于body进行偏离
一个是父亲没定位,直接相当body进行偏离
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.one{
margin-left: 200px;
width: 300px;
height: 300px;
background-color: red;
/* position: relative; */
}
.two{
width: 100px;
height: 100px;
position: absolute;
top: 20px;
left: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div class="one">
<div>
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
</div>
<div class="two">
</div>
</div>
</body>
</html>
给父盒子添加定位
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.one{
margin-left: 200px;
width: 300px;
height: 300px;
background-color: red;
position: relative;
}
.two{
width: 100px;
height: 100px;
position: absolute;
top: 20px;
left: 50px;
background-color: blue;
}
</style>
</head>
<body>
<div class="one">
<div>
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈
</div>
<div class="two">
</div>
</div>
</body>
</html>
filex:固定定位,脱离文档流,相对于视窗进行偏离
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.one{
margin-left: 200px;
width: 300px;
height: 300px;
background-color: red;
position: fixed;
top: 100px;
}
</style>
</head>
<body>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<div class="one">
</div>
</body>
</html>
2、textContent、innerText、innnerHTML的区别
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
button {
border: 1px solid red;
}
.mei{
display: none;
}
</style>
</head>
<body>
<div class="contain">
<span>啊啊啊啊</span>
<span class="mei">有么妹妹</span>
</div>
<button onclick="myFunction()">我是按钮</button>
<script>
function myFunction() {
console.log(event.type);
}
let container = document.querySelector("body");
console.log("textContent的内容是:", container.textContent);
console.log("innerText的内容是:", container.innerText);
console.log("innerHTML的内容是:", container.innerHTML);
</script>
</body>
</html>
textContent属性:可以获取指定节点的文本及其后代节点中文本内容,也包括<script>和<style>元素中的内容;会获取
display:none
的节点的文本innerText属性:也是获取指定节点的文本及其后代节点中文本内容,但不能获取<script>和<style>元素中的内容。不会获取
display:none
的节点的文本innerHTML属性:是获取HTML文本结构内容
3、watch,computed和methods的关系
watch和computed各自处理的数据关系场景不同
1.watch擅长处理的场景:一个数据影响多个数据
2.computed擅长处理的场景:一个数据受多个数据影响
4、==与===的区别
- 对于string,number等基础类型,== 和=== 是有区别的,不同类型间比较,== 之比较“转化成同一类型后的值”看“值”是否相等,=== 如果类型不同,其结果就是不等
- 同类型比较,直接进行“值”比较,两者结果一样
- 对于Array,Object等高级类型,== 和 ===是没有区别的
- 基础类型与高级类型, == 和 ===是有区别的
- 对于 ==,将高级转化为基础类型,进行“值”比较,因为类型不同, === 结果为false
简而言之就是 " == " 只要求值相等; " === " 要求值和类型都相等
5、 style标签写在body后与body前有什么区别?
- 写在body标签前利于浏览器逐步渲染:
- resources downloading->CSSOM+DOM->RenderTree(composite)->Layout->paint
- 写在body标签后:
- 由于浏览器以逐行方式对html文档进行解析;
- 当解析到写在尾部的样式表(外联或写在style标签)会导致浏览器停止之前的渲 染,等待加载且解析样式表完成之后重新渲染;
- 在windows的IE下可能会出现FOUC现象(即样式失效导致的页面闪烁问题);
6、display: none与visibility: hidden 的区别
很多前端的同学认为visibility: hidden和display: none的区别仅仅在于display: none隐藏后的元素不占据任何空间,而visibility: hidden隐藏后的元素空间依旧保留 ,实际上没那么简单,visibility是一个非常有故事性的属性
visibility具有继承性
给父元素设置visibility:hidden;子元素也会继承这个属性。但是如果重新给子元素设置visibility: visible,则子元素又会显示出来。这个和display: none有着质的区别
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.big{
visibility: hidden;
width: 300px;
height: 300px;
background-color: red;
}
.small{
width: 100px;
height: 100px;
visibility: visible;
background-color: blue;
}
</style>
</head>
<body>
<div class="main">
<div class="big">
<div class="small"></div>
</div>
</div>
</body>
</html>
visibility: hidden不会影响序号计数
visibility: hidden虽然让一个元素不见了,但是其计数器仍在运行。这和display: none完全不一样
<!DOCTYPE html>
<html lang="en">
<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>
<style>
.big{
visibility: hidden;
display: none;
width: 300px;
height: 300px;
background-color: red;
}
.small{
width: 100px;
height: 100px;
visibility: visible;
background-color: blue;
}
</style>
</head>
<body>
<div>
<strong>给元素设置visibility:hidden样式</strong>
<ol>
<li>元素1</li>
<li style="visibility:hidden;">元素2</li>
<li>元素3</li>
<li>元素4</li>
</ol>
</div>
<div>
<strong>给元素设置display:none样式</strong>
<ol>
<li>元素1</li>
<li style="display:none;">元素2</li>
<li>元素3</li>
<li>元素4</li>
</ol>
</div>
</body>
</html>
7、call、apply、bind使用与区别
三者都是可以改变this的指向
<!DOCTYPE html>
<html lang="en">
<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>
</head>
<body>
<script>
let fn = function (a, b) {
console.log(this, a, b);
}
let obj = { name: "obj" };
console.log("call-----------");
fn(2, 9)
fn.call(obj, 1, 2); // this:obj a:1 b:2
fn.call(1, 2); // this:1 a:2 b:undefined
fn.call(); // this:window a:undefined b:undefined
fn.call(null); // this=window a=undefined b=undefined
fn.call(undefined); // this=window a=undefined b=undefined
console.log("apply-----------");
// 第二个参数与call不同只能传数组
fn.apply(obj, [1, 2])
console.log("bind-----------");
// bind与call参数一致,但是call和apply是立即执行,bind必须绑定个事件来触发
document.onclick = fn.bind(obj,29,2);
</script>
</body>
</html>