jQuery----获取元素

本文介绍了如何使用JavaScript在前端开发中获取元素的尺寸,包括内容区域、内边距和外边距,并展示了计算元素偏移量的方法。通过实例演示了`width`, `height`, `innerWidth`, `innerHeight`, `outerWidth`, `outerHeight`和`offset()`方法的应用。
摘要由CSDN通过智能技术生成

获取元素的尺寸

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>获取元素的尺寸</title>
		<style>
			* {
				margin: 0;
				padding: 0;
			}

			div {
				width: 300px;
				height: 300px;
				padding: 20px;
				border: 20px solid #333;
				margin: 20px;
				background-color: aqua;
				background-clip: content-box;
			}
		</style>
	</head>
	<body>
		<div></div>

		<script src="../jquery.js"></script>
		<script>
			console.log('--------------获取元素尺寸--------------');
			// 第一套   width和height  获取的是元素内容区域的尺寸
			console.log($('div').width())
			console.log($('div').height())
			// 第二套   innerWidth和innerheight   获取的是padding
			console.log($('div').innerWidth());
			console.log($('div').innerHeight());
			// 第三套  outerWidth 和  outerHeight   获取的是整个元素的尺寸
			console.log($('div').outerWidth());
			console.log($('div').outerHeight());
		</script>
	</body>
</html>

获取元素的偏移量

// 获取元素相对于页面左上角的位置

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>获取元素的偏移量</title>
		<style>
			*{
				margin: 0;
				padding: 0;
				
			}
			div{
				background-color: red;
				width: 500px;
				height: 500px;
				margin: 30px;
				overflow: hidden;
			}
			p{
				width: 300px;
				height: 300px;
				background-color: orange;
				margin: 30px;
				overflow: hidden;
			}
			span{
				display: block;
				width: 100px;
				height: 100px;
				background-color: aqua;
				margin: 30px;
			}
		</style>
	</head>
	<body>
		<div>
			<p>
				<span></span>
			</p>
		</div>
	</body>
	
	<script src="../jquery.js"></script>
	<script>
		console.log('------获取元素偏移量-------');
		//  1. offset()
		// 获取元素相对于页面左上角的位置
		console.log('div:',$('div').offset());
		console.log('p:',$('p').offset());
		console.log('span:',$('span').offset())
	</script>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值