forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr_fix.js
94 lines (91 loc) · 4.25 KB
/
attr_fix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
define("attr_fix", !! top.getComputedStyle, ["$node"], function($) {
$.fixIEAttr = function(valHooks, attrHooks) {
var rattrs = /\s+([\w-]+)(?:=("[^"]*"|'[^']*'|[^\s>]+))?/g,
rquote = /^['"]/,
support = $.support,
defaults = {
checked: "defaultChecked",
selected: "defaultSelected"
}
$.fixDefault = function(node, name, value) {
var _default = defaults[name];
if(_default) {
node[_default] = value;
}
}
attrHooks["@ie:get"] = function(node, name) {
var str = node.outerHTML.replace(node.innerHTML, ""),
obj = {},
k, v;
while(k = rattrs.exec(str)) { //属性值只有双引号与无引号的情况
v = k[2]
obj[k[1].toLowerCase()] = v ? rquote.test(v) ? v.slice(1, -1) : v : ""
}
return obj[name];
}
attrHooks["@ie:set"] = function(node, name, value) {
var attr = node.getAttributeNode(name);
if(!attr) { //不存在就创建一个同名的特性节点
attr = node.ownerDocument.createAttribute(name);
node.setAttributeNode(attr);
}
attr.value = value + "";
}
if(!support.attrInnateValue) {
// http://gabriel.nagmay.com/2008/11/javascript-href-bug-in-ie/
//在IE6-8如果一个A标签,它里面包含@字符,并且没任何元素节点,那么它里面的文本会变成链接值
$.propHooks["href:set"] = attrHooks["href:set"] = function(node, name, value) {
var b
if(node.tagName == "A" && node.innerText.indexOf("@") > 0 && !node.children.length) {
b = node.ownerDocument.createElement('b');
b.style.display = 'none';
node.appendChild(b);
}
node.setAttribute(name, value + "");
if(b) {
node.removeChild(b);
}
}
}
//========================attrHooks 的相关修正==========================
if(!support.attrInnateHref) {
//http://msdn.microsoft.com/en-us/library/ms536429%28VS.85%29.aspx
//IE的getAttribute支持第二个参数,可以为 0,1,2,4
//0 是默认;1 区分属性的大小写;2取出源代码中的原字符串值(注,IE67对动态创建的节点没效),4用于取得完整路径
//IE 在取 href 的时候默认拿出来的是绝对路径,加参数2得到我们所需要的相对路径。
"href,src,width,height,colSpan,rowSpan".replace($.rword, function(method) {
attrHooks[method.toLowerCase() + ":get"] = function(node, name) {
var ret = node.getAttribute(name, 2);
return ret == null ? void 0 : ret;
}
});
"width,height".replace($.rword, function(attr) {
attrHooks[attr + ":set"] = function(node, name, value) {
node.setAttribute(attr, value === "" ? "auto" : value + "");
}
});
$.propHooks["href:get"] = function(node, name) {
return node.getAttribute(name, 4);
};
}
if(!document.createElement("form").enctype) { //如果不支持enctype, 我们需要用encoding来映射
$.propMap.enctype = "encoding";
}
if(!support.attrInnateStyle) {
//IE67是没有style特性(特性的值的类型为文本),只有el.style(CSSStyleDeclaration)(bug)
attrHooks["style:get"] = function(node) {
return node.style.cssText || undefined;
}
attrHooks["style:set"] = function(node, name, value) {
node.style.cssText = value + "";
}
}
//========================valHooks 的相关修正==========================
if(!support.attrInnateName) { //IE6-7 button.value错误指向innerText
valHooks["button:get"] = attrHooks["@ie:get"]
valHooks["button:set"] = attrHooks["@ie:set"]
}
delete $.fixIEAttr;
}
return $;
})