jQuery.support 的实现方式

[size=large][align=center][b]jQuery.support[/b][/align][/size]
jQuery.support 用于检查浏览器对各项特性的支持。检查项多达 27 个。
首先,让我们用一段代码测试一下 support 中包含的检查项:

<script src='jquery.js'></script>
<script>
support = $.support;
for (key in support) {
document.write('support.' + key + ' = ' + support[key] + '<br />');
}
</script>

IE 下的输出结果为:

support.leadingWhitespace = false
support.tbody = false
support.htmlSerialize = false
support.style = false
support.hrefNormalized = false
support.opacity = false
support.cssFloat = false
support.checkOn = true
support.optSelected = false
support.getSetAttribute = false
support.submitBubbles = false
support.changeBubbles = false
support.focusinBubbles = true
support.deleteExpando = false
support.noCloneEvent = false
support.inlineBlockNeedsLayout = false
support.shrinkWrapBlocks = true
support.reliableMarginRight = true
support.noCloneChecked = false
support.optDisabled = true
support.radioValue = false
support.checkClone = undefined
support.appendChecked = false
support.boxModel = false
support.reliableHiddenOffsets = false
support.ajax = true
support.cors = false

FireFox 中的显示结果为:

support.leadingWhitespace = true
support.tbody = true
support.htmlSerialize = true
support.style = true
support.hrefNormalized = true
support.opacity = true
support.cssFloat = true
support.checkOn = true
support.optSelected = true
support.getSetAttribute = true
support.submitBubbles = true
support.changeBubbles = true
support.focusinBubbles = false
support.deleteExpando = true
support.noCloneEvent = true
support.inlineBlockNeedsLayout = false
support.shrinkWrapBlocks = false
support.reliableMarginRight = true
support.noCloneChecked = true
support.optDisabled = true
support.radioValue = true
support.checkClone = undefined
support.appendChecked = true
support.boxModel = true
support.reliableHiddenOffsets = true
support.ajax = true
support.cors = true

需要注意的是,源代码中 checkClone 的检查是有问题的。这将在后面提到。
接下来,我们就对这些检查项进行逐一的分析。

[size=medium][align=center][b]leadingWhitespace[/b][/align][/size]
检查用 innerHTML 赋值时,是否会保留前面的空白符。
IE中为 false , Firefox 中为 true 。
实现方式如下:

$ = function(){
var div = document.createElement( "div" );
div.innerHTML = " <link/><table></table><a href='/a' style='top:1px;float:left;opacity:.55;'>a</a><input type='checkbox'/>";

var support = {
leadingWhitespace: (div.firstChild.nodeType === 3)
}

return { support : support }
}();

这段代码中,先创建了一个 div ,之后就用 innerHTML 为 div 赋值,
然后检查 div 的第一个子元素的 nodeType 是否为 3 (表示文本),
是的话则空白字符被保留,否则未被保留。

[size=medium][align=center][b]tbody[/b][/align][/size]
检查是否会自动为 table 插入 tbody ,会的话为 false ,不会则为 true 。
IE中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
leadingWhitespace: (div.firstChild.nodeType === 3),
tbody: !div.getElementsByTagName("tbody").length
}

// Other codes ...
}();

这段代码在原有基础上为 support 增加了 tbody 属性, 并检查是否能够获取 tbody 标签。
由于 innerHTML 中已经包含了 table 标签,能获取 tbody 标签的话则表示会自动为 table 插入 tbody,否则表示不会自动插入 tbody 。

[size=medium][align=center][b]htmlSerialize[/b][/align][/size]
检查link标签能否被正确地序列化。
IE中为 false , FireFox 中为true 。
实现方式如下:

$ = function(){
// Other codes ...

var a = div.getElementsByTagName("a")[0];

var support = {
// Other codes ...
tbody: !div.getElementsByTagName( "tbody" ).length,
htmlSerialize: !!div.getElementsByTagName("link").length
}

return { support : support }
}();

这段代码在原有基础上为 support 增加了 htmlSerialize 属性,并检查能否获取 link 标签。
由于 innerHTML 中已经包含了 link 标签,能通过getElementsByTagName 获取 link 标签的话,表示 link 可以被正确地序列化,否则表示 link 标签不能被正确地序列化。

[size=medium][align=center][b]style[/b][/align][/size]
检查是否能通过 “style” 属性获取 DOM Element 的样式。
IE中为 false , Firefox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var a = div.getElementsByTagName("a")[0];

var support = {
// Other codes ...
htmlSerialize: !!div.getElementsByTagName("link").length,
style: /top/.test(a.getAttribute("style"))
}

return { support : support }
}();

这段代码先是通过 getElementsByTagName("a") 获取表示链接的 DOM Element,
然后在原有基础上为 support 增加了 style 属性,并检查能否通过 getAttribute("style") 获取样式。

[size=medium][align=center][b]hrefNormalized[/b][/align][/size]
检查链接的 “href” 属性能否被正常地序列化。
IE中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
style: /top/.test(a.getAttribute("style")),
hrefNormalized: (a.getAttribute("href") === "/a")
}

return { support : support }
}();

这段代码通过 getAttribute("href") 来获取 “href” 属性的值,
能正确获取则表示链接的 “href”属性能够被正确地序列化。

[size=medium][align=center][b]opacity[/b][/align][/size]
检查 css 样式中的透明度设置能够被有效支持。
IE 中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
hrefNormalized: (a.getAttribute("href") === "/a"),
opacity: /^0.55$/.test(a.style.opacity)
}

return { support : support }
}();

这段代码通过 a.style.opacity 来获取透明度设置,并用正则表达式 /^0.55$/ 进行检查,通过检查表示 css 样式中的透明度能够被有效地支持,否则表示不能。

[size=medium][align=center][b]cssFloat[/b][/align][/size]
检查 css 样式中的 float 属性能够被有效支持。
IE 中为 false , FireFox中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
opacity: /^0.55$/.test(a.style.opacity),
cssFloat: !!a.style.cssFloat
}

return { support : support }
}();

这段代码通过 a.style.cssFloat 来获取 float 属性,并通过 !! 将属性转为 boolean 值(undefined 将被转为 false ,而其他值将被转为 true)。

[size=medium][align=center][b]checkOn[/b][/align][/size]
检查 chebox 的 value 是否为 “on”。
IE 和 FireFox 中都为true,而 Chrome 中则为 false 。
实现方式如下:

$ = function(){
// Other codes ...

var a = div.getElementsByTagName("a")[0],
input = div.getElementsByTagName("input")[0];

var support = {
// Other codes ...
cssFloat: !!a.style.cssFloat,
checkOn: (input.value === "on")
}

return { support : support }
}();

这段代码先通过 getElementsByTagName("input") 获取 checkbox,然后检查 checkbox 的 value 是否为 “on”。

[size=medium][align=center][b]optSelected[/b][/align][/size]
检查 select 中的第一个 option 能否被默认选中。
IE 中为 false , FireFox 中为 true 。

$ = function(){
// Other codes ...

var select = document.createElement("select");
var opt = select.appendChild(document.createElement("option"));

var support = {
// Other codes ...
checkOn: (input.value === "on"),
optSelected: opt.selected
}

return { support : support }
}();

这段代码先通过 createElement("select") 创建了一个 select,
然后将一个 “option” 添加到了 select 中。
接着在 support 中增加了属性 optSelected,检查 opt.selected 是否为true 。

[size=medium][align=center][b]getSetAttribute[/b][/align][/size]
检查能够功过 getAttribute("calssName") 和 setAttribute("className", "...") 来获取和设置 div 的 css class。
实际上只检查 setAttribute("className", "...")。
IE 中为 false , FireFox 中为 true 。

$ = function(){
// Other codes ...

div.setAttribute("className", "t");

var support = {
// Other codes ...
optSelected: opt.selected,
getSetAttribute: div.className !== "t"
}

return { support : support }
}();

这段代码通过 div.setAttribute("classsName", "t") 将 div 的 css class 设置为 “t”,然后在 support 中增加 getSetAttribute 属性, 检查 div 的 className 是否为 “t”。

[size=medium][align=center][b]submitBubbles, changeBubbles, focusinBubbles[/b][/align][/size]
检查 submit、change、focus 事件是否在“冒泡阶段”触发。实际上只针对 IE 进行检查。因为大多数浏览器(及IE9)使用 addEventListener 附加事件,函数的第三个参数 useCapture (是否在“捕捉阶段”触发事件)既可以为 false ,也可以为 true 。
而 IE (IE9之前)使用 attachEvent 函数附加事件,该函数无法指定在哪个阶段触发事件,一律都为“冒泡阶段”触发。

关于 Bubble Event 的更多内容,可以参考 iteye 里其他相关的文章:
[url]http://www.iteye.com/search?query=bubble+event&type=blog[/url]

实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
getSetAttribute: div.className !== "t",
submitBubbles: true,
changeBubbles: true,
focusinBubbles: false
}

if (div.attachEvent) {
for(var i in { submit: 1, change: 1, focusin: 1 }) {
var eventName = "on" + i;
var isSupported = (eventName in div);
if (!isSupported) {
div.setAttribute(eventName, "return;");
isSupported = ( typeof div[eventName] === "function" );
}
support[i + "Bubbles"] = isSupported;
}
}

return { support : support }
}();

首先,在 support 中增加 submitBubbles, changeBubbles, focusinBubbles,默认值分别为 true, true, false ,这是针对大多数浏览器的。
然后,针对IE (也就是存在 DOMElement.attachEvent 函数的情况),检查 "onXXX" 事件是否存在,以及能否通过 setAttribute(eventName, xxx)进行设置,可以的话就判断为“冒泡阶段”触发(即只要支持该事件,就判断为“冒泡阶段”触发)。

事实上,jQuery 中的 focusin 事件是在 focus 的基础上进行模拟的,浏览器并不支持该事件,所以 focusinBubbles 总是为 false。

从源代码中的注释来看,似乎还考虑到了跨站脚本攻击:
Short-circuiting here helps us to avoid an eval call (in setAttribute) which can cause CSP to go haywire.

大意是说在这里进行简短的检查(typeof div[eventName] === "function"),而不是直接用 eval 执行事件,可以避免不可控的跨站脚本攻击。(我不确定有没有翻译错)

[size=medium][align=center][b]deleteExpando[/b][/align][/size]
检查是否允许删除附加在 DOM Element 上的数据。
IE 中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
focusinBubbles: false,
deleteExpando: true
}

// Other codes ...

try {
delete div.test;
} catch(e) {
support.deleteExpando = false;
}

return { support : support }
}();

首先在 support 中增加属性 deleteExpando ,默认值为 true 。
然后尝试删除 div.test ,发生错误则将 deleteExpando 设为 false 。

[size=medium][align=center][b]noCloneEvent[/b][/align][/size]
检查复制 DOM Element 时是否会连同 event 一起复制,会则为 false , 不会则为true 。
IE 中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
deleteExpando: true,
noCloneEvent: true
}

// Other codes ...

if (!div.addEventListener && div.attachEvent && div.fireEvent) {
div.attachEvent( "onclick", function click() {
support.noCloneEvent = false;
div.detachEvent( "onclick", click );
});
div.cloneNode(true).fireEvent("onclick");
}

return { support : support }
}();

首先在 support 中增加属性 noCloneEvent , 默认值为 true 。
然后复制 div, 并触发其 “onclick” 事件,触发成功则为将 noCloneEvent 设为 false。
从判断条件来看,依旧是针对 IE 的事件体系的检查。

[size=medium][align=center][b]inlineBlockNeedsLayout, shrinkWrapBlocks[/b][/align][/size]
都是针对 offsetWidth 的检查。

inlineBlockNeedsLayout 表示将原本 display 为 block 的 DOM Element 设置为 disylay: inline 时,是否与 inline 形式的 DOM Elemnt 一致( offsetWidth 为 2 )。
IE 8 及之前的浏览器中为 true , FireFox 中为 false 。

shrinkWrapBlocks 表示内部 DOM Element 的样式是否会影响外部 DOM Element 的样式。
IE 6 中为 true , 多数浏览器中为 false 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
noCloneEvent: true,
inlineBlockNeedsLayout: false,
shrinkWrapBlocks: false
}

// Other codes ...

if ( "zoom" in div.style ) {
div.style.display = "inline";
div.style.zoom = 1;
support.inlineBlockNeedsLayout = ( div.offsetWidth === 2 );

div.style.display = "";
div.innerHTML = "<div style='width:4px;'></div>";
support.shrinkWrapBlocks = ( div.offsetWidth !== 2 );
}

return { support : support }
}();

首先在 support 中增加这两个属性,然后将 div 的 css 样式中的 display 设为 inline ,并检查 offsetWidth ,以确定 inlineBlockNeedsLayout 的值。
接着在 div 内部增加一个 div, 并将其宽度设为 4px ,并检查 offsetWith 的值,以确定外部的 div 是否会受到影响而收缩。

[size=medium][align=center][b]reliableMarginRight[/b][/align][/size]
检查 Margin Right 的计算是否可靠。 各浏览器中都为 true 。
原注释中提到某些老版本的 Webkit 内核的浏览器中为 false 。
实现方式如下:

$ = function(){
// Other codes ...

var support = {
// Other codes ...
shrinkWrapBlocks: false,
reliableMarginRight: true
}

// Other codes ...

if ( document.defaultView && document.defaultView.getComputedStyle ) {
var marginDiv = document.createElement( "div" );
marginDiv.style.width = "0";
marginDiv.style.marginRight = "0";
div.appendChild( marginDiv );
support.reliableMarginRight =
( parseInt( document.defaultView.getComputedStyle( marginDiv, null ).marginRight, 10 ) || 0 ) === 0;
}

return { support : support }
}();

简单地说,就是将 width 和 marginRight 设为 0 时,获取的 marginRignt 应为 0 。

[align=center][size=medium][b]noCloneChecked[/b][/size][/align]
检查复制 checkbox 时是否连选中状态也一同复制,若复制则为 false ,否则为 true 。
实现方式如下:

$ = function(){
// Other codes ...

input.checked = true;
support.noCloneChecked = input.cloneNode(true).checked;

return { support : support }
}();

这段代码将 input 的选中状态设为 true ,然后复制 input ,并检查 checked 是否为 true 。

[size=medium][align=center][b]optDisabled[/b][/align][/size]
已经被设为 disable 的 select ,其内部的 option 的 disable 不应为 true 。
这个名称有一定的误导性,可能称为 “optNotDisabled” 更合适一些。
在各浏览器上的值都为 true 。
根据原注释,某些老版本的 Webkit 内核的浏览器上,该值为 false 。
实现如下:

$ = function(){
// Other codes ...

select.disabled = true;
support.optDisabled = !opt.disabled;

return { support : support }
}();

这段代码先将 select 的状态设为 disable , 然后检查其中的 option 的 disable 的值。

[size=medium][align=center][b]radioValue[/b][/align][/size]
检查 input 元素被设为 radio 类型后是否仍然保持原来的值。
IE 中为 false , FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

input = document.createElement("input");
input.value = "t";
input.setAttribute("type", "radio");
support.radioValue = input.value === "t";

return { support : support }
}();

这段代码先创建了 input 元素,将 value 设为 “t” ,然后将其类型设置为 “radio”,最后检查器 input 原来的值是否仍然保留。

[size=medium][align=center][b]checkClone[/b][/align][/size]
检查 fragment 中的 checkbox 的选中状态是否能被复制,IE 中为 false ,FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

div.innerHTML = "";

input.setAttribute("checked", "checked");
div.appendChild( input );
fragment = document.createDocumentFragment();
fragment.appendChild( div.firstChild );

support.checkClone = fragment.cloneNode(true).cloneNode(true).lastChild.checked;

return { support : support }
}();

这段代码创建了一个 fragment ,并将一个处于选中状态的 checkbox 加入,连续复制两遍后检查 checkbox 是否为选中状态。(这里源代码中的实现似乎有问题,没有将 div 的 innerHTML 清空,导致 div 的 firstChild 并非 checkbox,使得 checkclone 在各浏览器中的值均为 undefined 。)

[align=center][size=medium][b]appendChecked[/b][/size][/align]
检查被添加到 DOM 中的 checkbox 是否仍然保留原来的选中状态。
IE 中为 false ,FireFox 中为 true 。
实现方式如下:

$ = function(){
// Other codes ...

support.appendChecked = input.checked;

return { support : support }
}();

实际上只是简单地检查之前添加到 fragment 中的 checkbox 的选中状态。

[align=center][size=medium][b]boxModel[/b][/size][/align]
检查页面渲染是否符合 W3C Box Model 。
在 IE 中没有 DocType 声明时为 false ,其余情况为 true 。
实现方式如下:

$ = function(){
// Other codes ...

div.innerHTML = "";
div.style.width = div.style.paddingLeft = "1px";

body = document.createElement( "body" );
bodyStyle = {
visibility: "hidden",
width: 0,
height: 0,
border: 0,
margin: 0,
background: "none"
};
for (var i in bodyStyle ) {
body.style[i] = bodyStyle[i];
}
body.appendChild(div);
document.documentElement.appendChild(body);

support.boxModel = div.offsetWidth === 2;

body.innerHTML = "";
document.documentElement.removeChild( body );

return { support : support }
}();

将 div 的 width 和 paddingLeft 设为 1px ,然后将它添加到 body 中,检查 div 的 offsetWidth 是否为 2 。

[align=center][size=medium][b]reliableHiddenOffsets[/b][/size][/align]
检查 hidden 状态下的 offsetWidth 和 offsetHeight 是否正确。
IE 中为 false , FireFox 中为 true 。
实现方式如下(插入到清空 body.innerHTML 之前):

$ = function(){
// Other codes ...

div.innerHTML = "<table><tr><td style='padding:0;border:0;display:none'></td><td>t</td></tr></table>";
var tds = div.getElementsByTagName("td");

var isSupported = (tds[0].offsetHeight === 0);

tds[0].style.display = "";
tds[1].style.display = "none";

support.reliableHiddenOffsets = isSupported && (tds[0].offsetHeight === 0);

body.innerHTML = "";
document.documentElement.removeChild( body );

return { support : support }
}();

检查将一个 td 隐藏时, 相邻的 td 的 offsetHeight 是否为 0 。

[size=medium][align=center][b]ajax, cors[/b][/align][/size]
检查是否支持 ajax 请求,以及是否支持跨域 ajax。
IE 与 FireFox 中 ajax 均为 true ,IE 中 cors 为 false , FireFox 中 cros 为 false 。
实现方式如下:

$ = function(){
// Other codes ...

var xhr = window.ActiveXObject ?
!this.isLocal && createStandardXHR() || createActiveXHR() :
createStandardXHR();

support.ajax = !!xhr;
support.cors = !!xhr && ( "withCredentials" in xhr )

function createStandardXHR() {
try {
return new window.XMLHttpRequest();
} catch( e ) {}
}

function createActiveXHR() {
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch( e ) {}
}

return { support : support }
}();

尝试创建 ajax 对象,创建成功,则 ajax 为 true ;如果 ajax 对象中包含了 "withCredentials" 属性,则表示支持跨域 ajax。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值