JS基础入门篇(二十四)—DOM(下)

news/2025/6/19 17:58:33

1.offsetLeft/offsetTop

offsetLeft/offsetTop : 到定位父级节点的距离.

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>div{padding: 20px;}#wrap{width: 100%;height: 50px;border: 1px solid black;position: absolute;}#inner{background: yellow;}#content{width: 200px;height: 100px;margin-left: 30px;background: red;position: absolute;left: 100px;border:10px solid black;}</style>
</head>
<body><div id="wrap"><div id="inner"><div id="content"></div></div></div><script>var content=document.getElementById("content");console.log(content.offsetLeft);//130console.log(content.offsetTop);//40</script>
</body>
</html>

2.node.getBoundingClientRect

    返回值是一个对象,包含了元素盒模型的详细信息(可视大小);取对象中详细的属性值(相对于浏览器可视区域)。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>body{margin: 0;}#wrap{position: relative;width:400px;height:400px;border: 1px solid #000;left: 100px;top: 200px;}#box{width:100px;height:150px;background-color:red;position: absolute;left: 100px;top: 200px;margin: 100px;padding: 10px;border: 30px solid #000;}
</style>
</head>
<body>
<div id="wrap"><div id="box"></div>
</div>
<!--node.getBoundingClientRect()返回值是一个对象,包含了元素盒模型的详细信息(可视大小);取对象中详细的属性值(相对于浏览器可视区域)
-->
<script>var box = document.getElementById("box");console.log( box.getBoundingClientRect() );console.log( box.getBoundingClientRect().left );//盒子 左边 距离 可视区 左边 的距离  301console.log( box.getBoundingClientRect().right);//盒子 右边 距离 可视区 左边 的距离  481console.log( box.getBoundingClientRect().top);//盒子 顶部 距离 可视区 顶部 的距离 ,这个页面的滚动会发生变化   501console.log( box.getBoundingClientRect().bottom);//盒子 底部 距离 可视区 顶部 的距离,这个页面的滚动会发生变化   731console.log( box.getBoundingClientRect().width);//盒子 可视 宽度(就是不包括margin) 180console.log( box.getBoundingClientRect().height);//盒子 可视 高度(就是不包括margin)230
</script>
</body>
</html>

3.getAtrribute:获取元素属性

<body><div id="box" class="div1" age=10></div><script>var box=document.getElementById("box");
//-------------行间 自定义 属性 用getAttribute可以取到------------console.log(box.getAttribute("age"));//"10"
//-------------行间 自定义 属性 用.和[] 取不到---------------------console.log(box.age);//undefinedconsole.log(box["age"]);//undefined//------------------------------------------------------------box.gender="woman";
//-------------js中 自定义 属性 用.和[]可以取到------------console.log(box.gender);//"woman"console.log(box["gender"]);//"woman"
//-------------js中 自定义 属性 用getAttribute 取不到---------------------console.log(box.getAttribute("gender"));//null</script>
</body>

3.setAttribute和removeAttribute

setAttribute:设置的自定义属性在行间。
removeAttribute:删除行间所在的自定义属性。

<body><img id="img" _src="./2.jpg" src="1.jpg"/><script>var img = document.getElementById("img");document.onclick = function(){img.setAttribute( "src", img.getAttribute("_src") );//点击页面后,将图片1换成图片2};img.setAttribute( "s", img.getAttribute("_src") );//在行间设置自定义属性 s="./2.jpg".console.log(img.getAttribute("s"));setTimeout(function(){img.removeAttribute( "s" );//页面打开1s后,删除行间设置的自定义属性。},1000)</script>
</body>

4.移入移出效果

移入当前数字,数字的前后背景变色。点击查看效果

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li><li>7</li><li>8</li><li>9</li><li>10</li>
</ul>
<body><script>var ul=document.getElementsByTagName("ul")[0];var li=ul.getElementsByTagName("li");var now=li[0];for(var i=0;i<li.length;i++){li[i].onmouseover=function () {//清除上一次的颜色now.style.background="";now.previousElementSibling && (now.previousElementSibling.style.background="");now.nextElementSibling && (now.nextElementSibling.style.background="");//给此次移上去的li添加颜色this.style.background="red";this.previousElementSibling && (this.previousElementSibling.style.background="pink");this.nextElementSibling && (this.nextElementSibling.style.background="pink");//将此次对应的li赋值给now。now就知道此次指的是哪个li。now=this;}}</script>
</body>
</html>

https://dhexx.cn/news/show-1628996.html

相关文章

SpringBoot + SpringBatch + Quartz整合定时批量任务

点击上方“Java基基”&#xff0c;选择“设为星标”做积极的人&#xff0c;而不是积极废人&#xff01;每天 14:00 更新文章&#xff0c;每天掉亿点点头发...源码精品专栏 原创 | Java 2021 超神之路&#xff0c;很肝~中文详细注释的开源项目RPC 框架 Dubbo 源码解析网络应用框…

mysql存储过程循环整个表_mysql存储过程,时间循环建表

delimiter // #定义标识符为双斜杠drop procedure if exists test; #如果存在test存储过程则删除create procedure test() #创建无参存储过程,名称为testbegindeclare tbname varchar(20);declare i int; …

x21能刷小米系统吗_小米系统是安卓系统中最强的吗?大家都错了,它是MIUI的进化版...

小米系统是安卓系统中最强的吗&#xff1f;大家都错了&#xff0c;它是MIUI的进化版现在国产手机做得越来越好了&#xff0c;在硬件方面国产手机比同级别的外国品牌要厚道太多了&#xff0c;所以在中国&#xff0c;外国品牌是没有任何生存空间的。在性价比方面他们是完败于国产…

AD批量添加群组与用户

AD批量添加群组与用户 一、背景 因管理需要计划将现有的serv-u服务改成adiisftpntfs架构&#xff0c;所以需要在ad中新建对应群组并添加用户。 为提高效率减少重复工作&#xff0c;编写一个批量新增群组及添加用户脚本&#xff0c;以下为测试环境。 二、汇总 注意事项&#xff…

之前写参数校验(Validator),差点被劝退...

点击上方“Java基基”&#xff0c;选择“设为星标”做积极的人&#xff0c;而不是积极废人&#xff01;每天 14:00 更新文章&#xff0c;每天掉亿点点头发...源码精品专栏 原创 | Java 2021 超神之路&#xff0c;很肝~中文详细注释的开源项目RPC 框架 Dubbo 源码解析网络应用框…

java seq_Seq命令的用法

Seq命令的用法用于产生从某个数到另外一个数之间的所有整数例一&#xff1a;# seq 1 10结果是1 2 3 4 5 67 8 9 10例二&#xff1a;#!/bin/bashfor i in seq 1 10;doecho $i;done或者用for i in $(seq 1 10)也可以seq-f, --formatFORMATuse printf style floating-point FORMAT…

Redis 分布式锁如何自动续期

点击上方“Java基基”&#xff0c;选择“设为星标”做积极的人&#xff0c;而不是积极废人&#xff01;每天 14:00 更新文章&#xff0c;每天掉亿点点头发...源码精品专栏 原创 | Java 2021 超神之路&#xff0c;很肝~中文详细注释的开源项目RPC 框架 Dubbo 源码解析网络应用框…

开源一个Android自定义图表库

类库接入使用示例 1、南丁格尔玫瑰图 NightingaleRoseChart2、占比饼状图表 PieChartLayout3、进度环形图 ProgressPieChart4、纵向柱状图 BarVerticalChart5、横向柱状图 BarHorizontalChart源码下载项目中有一些图表需求&#xff0c;一开始尝试使用一些开源的图表库&#xff…

python内置排序算法_python实现各种排序算法

排序算法是一种能将一串资料依照特定排序方式进行排列的一种算法。最常用到的排序方式是数值顺序以及字典顺序。虽然排序算法是一个简单的问题&#xff0c;但是从计算机科学发展以来&#xff0c;在此问题上已经有大量的研究&#xff0c;有用的新算法仍在不断的被发明。这些排序…

图解 ElasticSearch 原理

点击上方“Java基基”&#xff0c;选择“设为星标”做积极的人&#xff0c;而不是积极废人&#xff01;每天 14:00 更新文章&#xff0c;每天掉亿点点头发...源码精品专栏 原创 | Java 2021 超神之路&#xff0c;很肝~中文详细注释的开源项目RPC 框架 Dubbo 源码解析网络应用框…