JQuery:尺寸

介绍:
通过 jQuery,很容易处理元素和浏览器窗口的尺寸。
jQuery 提供多个处理尺寸的重要方法:width()、height()、innerHeight()、outerWidth()、outerHeight()

模型:

演练:
1、jQuery width() 和 height() 方法
width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。
下面的例子返回指定的 <div> 元素的宽度和高度:
实例:
$("button").click(function(){
  var txt="";
  txt+="Width: " + $("#div1").width() + "</br>";
  txt+="Height: " + $("#div1").height();
  $("#div1").html(txt);
});
代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script>
$(document).ready(function(){
//显示div元素高和宽
$("button").click(function(){
var txt = "";
txt += "Width:" + $("#div1").width() + "<br>";
txt += "Height:" + $("#div1").height();
$("#div1").html(txt);
});
});
</script> </head>
<body>
<div id="div1" style="width:200px;height:100px;background-color:red"></div>
<button>显示div元素高和宽</button>
<p>width():元素的宽</p>
<p>height():元素的高</p>
</body>
</html>

 

2、jQuery innerWidth() 和 innerHeight() 方法
innerWidth() 方法返回元素的宽度(包括内边距)。
innerHeight() 方法返回元素的高度(包括内边距)。
下面的例子返回指定的 <div> 元素的 inner-width/height:
实例:
$("button").click(function(){
  var txt="";
  txt+="Inner width: " + $("#div1").innerWidth() + "</br>";
  txt+="Inner height: " + $("#div1").innerHeight();
  $("#div1").html(txt);
});
代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script>
$(document).ready(function(){
//显示div元素尺寸
$("button").click(function(){
var txt = "";
txt += "Width:" + $("#div1").innerWidth() + "<br>";
txt += "Height:" + $("#div1").innerHeight();
$("#div1").html(txt);
});
});
</script> </head>
<body>
<div id="div1" style="width:200px;height:100px;padding:10px;margin:3px;background-color:red"></div>
<button>显示div元素尺寸</button>
<p>innerWidth() - 返回元素的宽度 (包含内边距)</p>
<p>innerHeight() - 返回元素的高度 (包含内边距)</p>
</body>
</html>

 

3、jQuery outerWidth() 和 outerHeight() 方法
outerWidth() 方法返回元素的宽度(包括内边距和边框)。
outerHeight() 方法返回元素的高度(包括内边距和边框)。
下面的例子返回指定的 <div> 元素的 outer-width/height:
实例
$("button").click(function(){
  var txt="";
  txt+="Outer width: " + $("#div1").outerWidth() + "</br>";
  txt+="Outer height: " + $("#div1").outerHeight();
  $("#div1").html(txt);
});
代码如下:

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<title>JQuery的使用!!!</title>
<script src="jquery-3.1.0.js"></script>
<script>
$(document).ready(function(){
//显示div元素尺寸
$("button").click(function(){
var txt = "";
txt += "Width:" + $("#div1").outerWidth() + "<br>";
txt += "Height:" + $("#div1").outerHeight();
$("#div1").html(txt);
});
});
</script> </head>
<body>
<div id="div1" style="width:200px;height:100px;padding:10px;margin:3px;border:1px solid green;background-color:red"></div>
<button>显示div元素尺寸</button>
<p>outerWidth() - 返回元素的宽度 (包括内边距和边框)</p>
<p>outerHeight() - 返回元素的高度 (包括内边距和边框)</p>
</body>
</html>

 

JQuery:JQuery的尺寸的更多相关文章

  1. 什么是jquery $ jQuery对象和DOM对象 和一些选择器

    1什么是jQuery: jQuery就是将一些方法封装在一个js文件中.就是个js库 我们学习这些方法. 2为什么要学习jQuery: 原生js有以下问题: 1.兼容性问题2.代码重复3.DOM提供的 ...

  2. (转)整体把握jQuery -jQuery 的原型关系图

    整体把握jQuery -jQuery 的原型关系图 (原)http://www.html5cn.org/article-6529-1.html 2014-7-2 17:12| 发布者: html5cn ...

  3. jQuery: jquery.json.js

    http://api.jquery.com/jQuery.parseJSON/ http://www.json.org/json-zh.html http://fineui.codeplex.com/ ...

  4. JQuery --- 第一期 (初识jQuery, JQuery核心函数和工具方法)

    个人学习笔记  初识jQuery 1.我的第一个JQuery <!DOCTYPE html> <html lang="en"> <head> & ...

  5. jQuery中的尺寸及位置的取和设

    1.offset(); 获取位置值: $(selector).offset().left; $(selector).offset().top; 设置位置值: $(selector).offset({t ...

  6. jquery 操作css 尺寸

    .height() 获取元素集合中的第一个元素的当前计算高度值,或设置每一个匹配元素的高度值. .height() 获取匹配元素集合中的第一个元素的当前计算高度值. 这个方法不接受参数. $(wind ...

  7. jQuery的相关尺寸获取 - 学习笔记

    获取元素相对于文档的偏移量 获取当前元素相对于父级元素的偏移量 获取文档滚动距离 获取元素的宽度和高度 设置元素的宽度和高度 获取可视区域的宽度和高度 获取文档的宽度和高度 获取元素相对于文档的偏移量 ...

  8. jquery Jquery 遍历 获取设置 效果

    speed: slow fast 毫秒 隐藏 显示 $(selector).hide(speed,callback) 隐藏. $(selector).show(speed,callback) 显示 $ ...

  9. [jQuery]jQuery DataTables插件自定义Ajax分页实现

    前言 昨天在博客园的博问上帮一位园友解决了一个问题,我觉得有必要记录一下,万一有人也遇上了呢. 问题描述 园友是做前端的,产品经理要求他使用jQuery DataTables插件显示一个列表,要实现分 ...

  10. jQuery jQuery对象与dom对象的转换

    jQuery对象本质上是一个构造函数,主要作用是返回jQuery对象的实例. jQuery选择器返回的是一个类似数组的对象,用下标运算取出的对象如$('body')[0],是dom对象,对此jquer ...

随机推荐

  1. Qt Examples Qt实例汇总

    ActiveQt Examples Using ActiveX from Qt applications. Animation Framework Examples Doing animations ...

  2. linux中执行命令权限不够怎样处理

    在linux中执行命令权限不够就要增加权限,先看遇到的情况 查看权限情况 那就赋予权限 执行命令

  3. ASP.NET开发中主要的字符验证方法-JS验证、正则表达式、验证控件、后台验证

    ASP.NET开发中主要的字符验证方法-JS验证.正则表达式.验证控件.后台验证 2012年03月19日 星期一 下午 8:53 在ASP.NET开发中主要的验证方法收藏 <1>使用JS验 ...

  4. Html - Bootstrap Panel面板

    http://v3.bootcss.com/components/#panels Bootstrap Panel面板 <div class="panel panel-default&q ...

  5. Acronis True Image Home 2011 PXE服务器配置_qxxz_新浪博客

    想实现网络启动,并且Acronis启动菜单中带有Acronis True Image Home,需要安装以下软件: 1.安装Acronis True Image Home 2011及plush pac ...

  6. JAVA WEB 的JSP(9*9乘法表+*型金字塔)

    运行环境及工具: (Tomcat7) + (JAVA JDK)+ (Eclipse for J2EE) 输出9*9乘法表 代码片段的练习 增加一些简单的JS功能 <%@ page import= ...

  7. sql order by+字段,指定按照哪个字段来排序

    1.我们就可以使用 MySQL 的 ORDER BY 子句来设定你想按哪个字段哪中方式来进行排序,再返回搜索结果. 2.SELECT field1, field2,...fieldN table_na ...

  8. UITableview cell中多个按钮

    xib的 //不使用这种- (IBAction)button:(UIButton *)sender; //使用这种 @property (weak, nonatomic) IBOutlet UIBut ...

  9. 第二章、 Linux 如何学习

    第二章. Linux 如何学习 最近更新日期:2009/08/06 1. Linux当前的应用角色 1.1 企业环境的利用 1.2 个人环境的使用 Linux当前的应用角色 在第一章Linux是什么当 ...

  10. Yii源码阅读笔记(四)

    所有控制器action的基类yii\base\Action.php namespace yii\base;//定义的命名空间 use Yii //使用的命名空间 class Action extend ...