jquery获取元素宽高

前言

在这里插入图片描述

jquery获取元素宽高

<!DOCTYPE html>
<html lang="zh" >
<head>
	<meta charset="utf-8">
	<style>
	  li {
		margin: 10px;
		padding: 5px;
		border: 2px solid #666;
		width: 800px;
		height: 100px;
		display: block;
	  }
	  li:first-child {
		background-color:#0fc;
	  }
	  li:nth-child(2) {
		background-color:#cf0;
	  }
	  li:nth-child(3) {
		background-color:#cfc;
	  }
	  li:last-child {
		background-color:#3f0;
	  }
	  li:nth-last-child(2) {
		background-color:#333;
	  }
	</style>
</head>
<body class="white-bg">
	<ul>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
		<li></li>
	</ul>


<script src="https://lib.baomitu.com/jquery/3.5.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
	$("li:eq(0)").text("width:" + $("li:eq(0)").width() 
				  + ",innerWidth:" + $("li:eq(0)").innerWidth() 
				  + ",outerWidth:" + $("li:eq(0)").outerWidth() 
				  + ",outerWidth(true):" + $("li:eq(0)").outerWidth(true));
	$("li:eq(1)").text("width:" + $("li:eq(1)").height() 
				  + ",innerHeight:" + $("li:eq(1)").innerHeight() 
				  + ",outerHeight:" + $("li:eq(1)").outerHeight() 
				  + ",outerHeight(true):" + $("li:eq(1)").outerHeight(true));
});
</script>
</body>
</html>

参考

https://www.cnblogs.com/goloving/p/7113567.html
https://api.jquery.com/width/
https://api.jquery.com/innerWidth/
https://api.jquery.com/outerWidth/
https://api.jquery.com/

要通过JavaScript获取页面图片的宽高大小,可以使用不同的方法。一种常见的方法是使用jQuery的`.width()`和`.height()`方法来获取图片的宽度和高度。这些方法可以直接应用于图片元素,例如`$('img').width()`和`$('img').height()`。\[2\] 另一种方法是使用JavaScript的`.offsetWidth`和`.offsetHeight`属性来获取图片的宽度和高度。这些属性返回的是元素在页面中的实际宽度和高度,包括边框和内边距。例如,可以通过`document.querySelector('img').offsetWidth`和`document.querySelector('img').offsetHeight`来获取图片的宽度和高度。\[2\] 还有一种方法是直接通过图片元素的属性值`.width`和`.height`来获取宽度和高度。如果图片元素没有设置这些属性,它们将返回0。例如,可以通过`document.querySelector('img').width`和`document.querySelector('img').height`来获取图片的宽度和高度。\[3\] 需要注意的是,使用这些方法获取图片的宽高大小时,确保在图片加载完成后再进行获取,以避免获取到不准确的值。可以使用`window.onload`事件或者`img.onload`事件来确保图片已经加载完成。\[2\] #### 引用[.reference_title] - *1* *2* [Android 获取图片真实宽高](https://blog.csdn.net/weixin_35690047/article/details/112889680)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [JS获取img图片的原始尺寸高度和宽度](https://blog.csdn.net/mouday/article/details/124845668)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值