JQuery擷取各種寬度、高度(format函數)執行個體_jquery

來源:互聯網
上載者:User

複製代碼 代碼如下:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>擷取頁面寬度</title>
    <script src="http://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=$("#div1");
                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());
                $("#div1").html(txt);
            });
        });
</script>
</head>
<body>

<div id="div1" style="height:auto;width:300px;padding:10px;margin:3px;border:1px solid blue;background-color:lightblue;"></div>
<br/>
<button>顯示當前各種尺寸</button>
    <p><a href="http://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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.