jquery源码03 (3184 , 3295) support : 功能检测

简介:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="jquery-2.0.3.js"></script>
<script>

$(function(){
    for(var attr in $.support){
        $('body').append( '<div>'+attr+':'+$.support[attr]+'</div>' );
    }
    /* 根据这些属性,判断浏览器类型
    checkOn:true
    optSelected:true
    reliableMarginRight:true
    boxSizingReliable:true
    pixelPosition:false
    noCloneChecked:true
    optDisabled:true
    radioValue:true
    checkClone:true
    focusinBubbles:false
    clearCloneStyle:true
    cors:true
    ajax:true
    */
    
    
    var oDiv = document.getElementById('div1');
    oDiv.onfocusin = function(){//鼠标移入事件
        alert(123);
    };
    var div = document.createElement('div');
    div.style.backgroundColor = 'red';
    div.cloneNode(true).style.backgroundColor = '';
    alert( div.style.backgroundColor );
    
    var div = $('<div>');
    div.css('backgroundColor','red');
    div.clone().css('backgroundColor','');
    alert( div.css('backgroundColor') );
});

</script>
</head>

<body>
<div id="div1">
<input type="text">
</div>
</body>
</html>
复制代码
复制代码
//兼容性,support是做兼容性检测,检测完之后实施是通过钩子机制。
jQuery.support = (function( support ) {//右边函数会立即执行
    var input = document.createElement("input"),
        fragment = document.createDocumentFragment(),//文档碎片
        div = document.createElement("div"),
        select = document.createElement("select"),
        opt = select.appendChild( document.createElement("option") );

    // input.type默认是text,基本都有,
    if ( !input.type ) {
        return support;
    }
    //改成复选框
    input.type = "checkbox";

    //新版本value的值是'on',老版本value的值是'',所以新本版support.checkOn=true,老版本support.checkOn=false,就可以通过support.checkOn属性值进行浏览器类型判断。
    support.checkOn = input.value !== "";

    // 狐火chrome下拉子项默认是选中的,ie默认不选中。
    support.optSelected = opt.selected;

    // 定义初始值
    support.reliableMarginRight = true;
    support.boxSizingReliable = true;
    support.pixelPosition = false;

    // Make sure checked status is properly cloned
    // Support: IE9, IE10
    //先让input选中,然后克隆,再判断选中状态,火狐chromeIE10以上都是选中的,ie低版本不是选中。
    input.checked = true;
    support.noCloneChecked = input.cloneNode( true ).checked;

    // Make sure that the options inside disabled selects aren't marked as disabled
    // (WebKit marks them as disabled)
    //下拉菜单禁止,子项有没有禁止。
    select.disabled = true;
    support.optDisabled = !opt.disabled;

    // Check if an input maintains its value after becoming a radio
    // Support: IE9, IE10
    input = document.createElement("input");
    input.value = "t";
    input.type = "radio";
    support.radioValue = input.value === "t";

    // #11217 - WebKit loses check when the name is after the checked attribute
    input.setAttribute( "checked", "t" );
    input.setAttribute( "name", "t" );

    fragment.appendChild( input );

    // Support: Safari 5.1, Android 4.x, Android 2.3
    // old WebKit doesn't clone checked state correctly in fragments
    support.checkClone = fragment.cloneNode( true ).cloneNode( true ).lastChild.checked;

    // Support: Firefox, Chrome, Safari
    // Beware of CSP restrictions (https://developer.mozilla.org/en/Security/CSP)
    support.focusinBubbles = "onfocusin" in window;

    //首先定义一个div的背景剪切是内容剪切,然后复制一份设置背景剪切为空,然后看原来div的背景剪切还是不是内容剪切。
    div.style.backgroundClip = "content-box";
    div.cloneNode( true ).style.backgroundClip = "";
    support.clearCloneStyle = div.style.backgroundClip === "content-box";

    //下面的需要等dom加载完才能检测
    jQuery(function() {
        var container, marginDiv,
            // Support: Firefox, Android 2.3 (Prefixed box-sizing versions).
            //
            divReset = "padding:0;margin:0;border:0;display:block;-webkit-box-sizing:content-box;-moz-box-sizing:content-box;box-sizing:content-box",
            body = document.getElementsByTagName("body")[ 0 ];

        if ( !body ) {
            // Return for frameset docs that don't have a body
            return;
        }
        //创建一个container来检测
        container = document.createElement("div");
        container.style.cssText = "border:0;width:0;height:0;position:absolute;top:0;left:-9999px;margin-top:1px";

        // Check box-sizing and margin behavior.
        body.appendChild( container ).appendChild( div );
        div.innerHTML = "";
        // Support: Firefox, Android 2.3 (Prefixed box-sizing versions).
        div.style.cssText = "-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;padding:1px;border:1px;display:block;width:4px;margin-top:1%;position:absolute;top:1%";

        // Workaround failing boxSizing test due to offsetWidth returning wrong value
        // with some non-1 values of body zoom, ticket #13543
        jQuery.swap( body, body.style.zoom != null ? { zoom: 1 } : {}, function() {
            support.boxSizing = div.offsetWidth === 4;
        });

        // Use window.getComputedStyle because jsdom on node.js will break without it.
        if ( window.getComputedStyle ) {
            support.pixelPosition = ( window.getComputedStyle( div, null ) || {} ).top !== "1%";
            support.boxSizingReliable = ( window.getComputedStyle( div, null ) || { width: "4px" } ).width === "4px";

            // Support: Android 2.3
            // Check if div with explicit width and no margin-right incorrectly
            // gets computed margin-right based on width of container. (#3333)
            // WebKit Bug 13343 - getComputedStyle returns wrong value for margin-right
            marginDiv = div.appendChild( document.createElement("div") );
            marginDiv.style.cssText = div.style.cssText = divReset;
            marginDiv.style.marginRight = marginDiv.style.width = "0";
            div.style.width = "1px";

            support.reliableMarginRight =
                !parseFloat( ( window.getComputedStyle( marginDiv, null ) || {} ).marginRight );
        }

        body.removeChild( container );
    });

    return support;//返回json
})( {} );//实参json
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/6924756.html,如需转载请自行联系原作者

相关文章
|
JavaScript
jQuery实现判断li的个数从而实现其他功能
jQuery实现判断li的个数从而实现其他功能
|
JavaScript
jquery实现的网页版扫雷小游戏源码
这是一款基于jQuery实现的经典扫雷小游戏源码,玩家根据游戏规则进行游戏,末尾再在确定的地雷位置单击右键安插上小红旗即可赢得游戏!是一款非常经典的jQuery游戏代码。本源码改进了获胜之后的读数暂停功能。
295 69
jQuery+CSS3实现404背景游戏动画源码
jQuery+CSS3实现404背景游戏动画源码
239 22
|
JavaScript
jQuery仿Key社游戏风格右键菜单特效源码
jQuery二次元风格右键菜单插件HTML源码,该插件将原生的浏览器右键菜单转换为一个动画的圆形菜单,并且带音效,效果非常的炫酷。 本段代码兼容目前最新的各类主流浏览器,是一款非常优秀的特效源码。
126 18
jQuery+Slick插件实现游戏人物轮播展示切换源码
jQuery+Slick插件实现游戏人物轮播展示切换源码
204 14
|
JavaScript
jQuery仿方块人物头像消除游戏源码
jQuery人物头像迷阵消除游戏代码是一款类似《宝石迷阵》类的方块消除类型的小游戏源码。
1324 13
|
JavaScript
jQuery+HTML5实现的微信大转盘抽奖源码
这是一款基于jQuery+HTML5实现的微信大转盘抽奖效果源码,是一款可配置奖品抽奖的jQuery大转盘抽奖代码,可实现点击按钮转轮旋转实现抽奖功能,效果逼真自然,代码里面有详细的注释,可以修改文字或者二次开发都可以
355 11
|
JavaScript
jQuery实现的卡片式翻转时钟HTML源码
jQuery实现的卡片式翻转时钟HTML源码
153 0
jQuery实现的卡片式翻转时钟HTML源码
|
JavaScript 前端开发 容器
jQuery多功能滑块插件r-slider.js
r-slider.js是一款jQuery多功能滑块插件。使用该插件,可以制作出滑块、开关按钮、进度条、向导步骤等多种效果。
220 5
|
JavaScript
jQuery实现的滚动切换图表统计特效源码
jQuery实现的滚动切换图表统计特效源码是一段全屏滚动的企业当月运营报告数据统计图表代码,涵盖流行的线性、圆形、柱形图统计方式,适应于绝大多数企业,欢迎感兴趣的朋友前来下载参考。
115 2