js实例教程-JQuery获取各种宽度、高度(format函数)实例

发布时间:2018-11-25 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了js实例教程-JQuery获取各种宽度、高度(format函数)实例脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。

代码如下:

<htML XMlns="https://www.w3.org/1999/xhtml">
<head>
<tITle>获取页面度</title>
<script src="https://ajax.GOOGLEapis.COM/ajax/libs/jquery/1.8.3/jquery.min.js" tyPE="text/javascript"></script>
<script type="text/javascript">
$.format = function (source, params) {
if (arguments.length == 1)
return function () {
VAR args = $.makeArray(arguments);
args.unshift(source);
return $.format.apply(this, args);
};
if (arguments.length > 2 && params.constructor != Array) {
params = $.makeArray(arguments).slice(1);
}
if (params.constructor != Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("//{" + i + "//}", "g"), n);
});
return source;
};
/*------------以上是字符串format函数----------------*/
$(document).ready(function () {
$("button").click(function () {
var d=$("#p1");
var txt = "";
txt += $.format("width(): {0}</br>", d.width());
txt += $.format("height(): {0}</br>", d.height());
txt += $.format("Inner Width: {0}</br>", d.innerWidth());
txt += $.format("Inner Height: {0}</br>", d.innerHeight());
txt += $.format("Outer Width: {0}</br>", d.outerWidth());
txt += $.format("Outer Height: {0}</br>", d.outerHeight());
txt += $.format("outerWidth(true): {0}</br>", d.outerWidth(true));
txt += $.format("outerHeight(true): {0}</br>", d.outerHeight(true));
txt += $.format("HTML文档宽度: {0}</br>", $(document).width());
txt += $.format("HTML文档高度: {0}</br>", $(document).height());
txt += $.format("浏览器视口宽度: {0}</br>", $(window).width());
txt += $.format("浏览器视口高度: {0}</br>", $(window).height());
$("#p1").html(txt);
});
});
</script>
</head>
<body>

<p id="p1" style="height:auto;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></p>
<br/>
<button>显示当前各种尺寸</button>
<p><a href="https://4welove.taobao.com" target="_blank">手机话费、Q币、游戏充值</a></p>
<p>width() - 返回元素的宽度。</p>
<p>height() - 返回元素的高度。</p>
<p>innerWidth() 方法返回元素的宽度(包括内边距)。 </p>
<p>innerHeight() 方法返回元素的高度(包括内边距)。 </p>
<p>outerWidth() 方法返回元素的宽度(包括内边距和边框)。 </p>
<p>outerHeight() 方法返回元素的高度(包括内边距和边框)。 </p>
<p>outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距)。 </p>
<p>outerHeight(true) 方法返回元素的高度(包括内边距、边框和外边距)。 </p>
<p>返回文档(HTML 文档)$(document).height()的高度</p>
<p>返回窗口(浏览器视口)$(window).height()的高度</p>
</body>
</html>


代码如下:

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<title>获取页面宽度</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.format = function (source, params) {
if (arguments.length == 1)
return function () {
var args = $.makeArray(arguments);
args.unshift(source);
return $.format.apply(this, args);
};
if (arguments.length > 2 &amp;& params.constructor != Array) {
params = $.makeArray(arguments).slice(1);
}
if (params.constructor != Array) {
params = [params];
}
$.each(params, function (i, n) {
source = source.replace(new RegExp("//{" + i + "//}", "g"), n);
});
return source;
};
/*------------以上是字符串format函数----------------*/
$(document).ready(function () {
$("button").click(function () {
var d=$("#p1");
var txt = "";
txt += $.format("width(): {0}</br>", d.width());
txt += $.format("height(): {0}</br>", d.height());
txt += $.format("Inner Width: {0}</br>", d.innerWidth());
txt += $.format("Inner Height: {0}</br>", d.innerHeight());
txt += $.format("Outer Width: {0}</br>", d.outerWidth());
txt += $.format("Outer Height: {0}</br>", d.outerHeight());
txt += $.format("outerWidth(true): {0}</br>", d.outerWidth(true));
txt += $.format("outerHeight(true): {0}</br>", d.outerHeight(true));
txt += $.format("HTML文档宽度: {0}</br>", $(document).width());
txt += $.format("HTML文档高度: {0}</br>", $(document).height());
txt += $.format("浏览器视口宽度: {0}</br>", $(window).width());
txt += $.format("浏览器视口高度: {0}</br>", $(window).height());
$("#p1").html(txt);
});
});
</script>
</head>
<body>

<p id="p1" style="height:auto;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></p>
<br/>
<button>显示当前各种尺寸</button>
<p><a href="https://4welove.taobao.com" target="_blank">手机话费、Q币、游戏充值</a></p>
<p>width() - 返回元素的宽度。</p>
<p>height() - 返回元素的高度。</p>
<p>innerWidth() 方法返回元素的宽度(包括内边距)。 </p>
<p>innerHeight() 方法返回元素的高度(包括内边距)。 </p>
<p>outerWidth() 方法返回元素的宽度(包括内边距和边框)。 </p>
<p>outerHeight() 方法返回元素的高度(包括内边距和边框)。 </p>
<p>outerWidth(true) 方法返回元素的宽度(包括内边距、边框和外边距)。 </p>
<p>outerHeight(true) 方法返回元素的高度(包括内边距、边框和外边距)。 </p>
<p>返回文档(HTML 文档)$(document).height()的高度</p>
<p>返回窗口(浏览器视口)$(window).height()的高度</p>
</body>
</html>


觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!

脚本宝典总结

以上是脚本宝典为你收集整理的js实例教程-JQuery获取各种宽度、高度(format函数)实例全部内容,希望文章能够帮你解决js实例教程-JQuery获取各种宽度、高度(format函数)实例所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。