U.MB.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //#region 浏览器兼容使用
  2. /*
  3. 设置兼容性的写法 ie里面有innerText,outerHTML,而HTML5没有这些属性,为了使得TML5兼容ie特性,写了如下兼容js,HTML5也在逐步形成上诉函数。
  4. */
  5. Namespace.register("U.MB")
  6. //通过修改浏览器内置属性 设置兼容,添加兼容,此兼容模块兼容ie6以上,firefox,safiry,chrome
  7. U.MB.init = function () {
  8. //如果是html5浏览器,则执行判断内的内容,ie则不用进入下面内容。
  9. if (window.HTMLElement) {
  10. HTMLElement.prototype.__defineGetter__("onmousewheel", function () {
  11. return this.onwheel;
  12. });
  13. HTMLElement.prototype.__defineSetter__("onmousewheel", function (UCB) {
  14. this.onwheel = UCB;
  15. });
  16. if (!("onmousewheel" in HTMLElement)) {
  17. if ("onwheel" in HTMLElement.prototype) {
  18. U.M.HEPropert(HTMLElement.prototype, "onmousewheel", function () {
  19. return this.onwheel;
  20. },
  21. function (UCB) {
  22. this.onwheel = UCB;
  23. });
  24. }
  25. else {
  26. //firefox支持onmousewheel
  27. if (browser.firefox) {
  28. (function () {
  29. window.addEventListener("DOMMouseScroll", function (UE) {
  30. //监控Scroll事件
  31. var _UOE = null
  32. , _UDOD = UE.srcElement;
  33. do {
  34. _UOE = $(_UDOD).attr("onmousewheel") || _UDOD.onmousewheel;
  35. _UDOD = $(_UDOD).Parent();
  36. } while (!(_UOE || !_UDOD)); //冒泡的顶部获取
  37. if (_UOE) {
  38. (typeof _UOE == "string") && (_UDOD.onmousewheel = new Function(_UOE));
  39. U.M.StopDefault();
  40. //阻止页面固定事件
  41. setTimeout(function () {
  42. _UOE.call(this, UE);
  43. }
  44. , 0);
  45. //异步执行 解决锁定的bug
  46. }
  47. }
  48. , false);
  49. }
  50. )();
  51. }
  52. }
  53. }
  54. if (!("innerText" in HTMLElement.prototype)) {/*检测浏览器是否支持innerText这个方法*/
  55. U.M.HEPropert(HTMLElement.prototype, "innerText",
  56. function () {
  57. return this.textContent;
  58. },
  59. function (UDE) {
  60. this.textContent = UDE;
  61. }
  62. );
  63. }
  64. if (!("outerHTML" in HTMLElement.prototype)) {/*检测浏览器是否支持outerHTML这个方法*/
  65. U.M.HEPropert(HTMLElement.prototype, "outerHTML",
  66. function () {
  67. var _UDOD = this.cloneNode(true),
  68. _UDTD = $$("div", {});
  69. _UDTD.appendChild(_UDOD);
  70. return _UDTD.innerHTML; /*如果不支持就使用innerhtml插入*/
  71. },
  72. function (UTH) {
  73. var i, _UDOD = $(this), _UDPD = _UDOD.Parent(), _UDCD = $$("div", {
  74. "innerHTML": UTH
  75. }).childNodes, _UDTD = document.createDocumentFragment;
  76. for (i = 0; i < _UDCD.length; i++) {
  77. _UDTD.appendChild(_UDCD[i]);
  78. }
  79. ; _UDTD.insertBefore(_UDTD, this);
  80. _UDOD.remove();
  81. }
  82. );
  83. }
  84. if (!("outerText" in HTMLElement.prototype)) {/*检测浏览器是否支持outerText这个方法*/
  85. U.M.HEPropert(HTMLElement.prototype, "outerText",
  86. function () {
  87. var _UDOD = this.cloneNode(true)
  88. , _UDTD = $$("div", {});
  89. _UDTD.appendChld(_UDOD);
  90. return _UDTD.innerText; /*如果不支持就使用innerText插入*/
  91. },
  92. function (UTH) {
  93. var i, _UDOD = $(this), _UDPD = _UDOD.Parent(), _UDCD = $$("div", {
  94. "innerText": UTH
  95. }).childNodes, _UDTD = document.createDocumentFragment;
  96. for (i = 0; i < _UDCD.length; i++) {
  97. _UDTD.appendChild(_UDCD[i]);
  98. }
  99. _UDTD.insertBefore(_UDTD, this);
  100. _UDOD.remove();
  101. });
  102. }
  103. if (!("componentFromPoint" in HTMLElement.prototype)) {
  104. U.M.HEPropert(HTMLElement.prototype, "componentFromPoint", function () {
  105. return function () {
  106. var _UE = U.M.GetMousep();
  107. var _UDE = U.M.GetElementAllInfo(this);
  108. if ((_UE["X"] > _UDE["CW"] + _UDE["TX"]) || (_UE["Y"] > _UDE["CH"] + _UDE["TY"])) {
  109. return "outside";
  110. }
  111. return "";
  112. }
  113. },
  114. function () { });
  115. }
  116. if (!("currentStyle" in HTMLElement.prototype)) {
  117. U.M.HEPropert(HTMLElement.prototype, "currentStyle", function () {
  118. return getComputedStyle(this, false);
  119. },
  120. function () { });
  121. }
  122. if (!("onreadystatechange" in HTMLElement.prototype)) {
  123. U.M.HEPropert(HTMLElement.prototype, "onreadystatechange", function () {
  124. return this.onload;
  125. },
  126. function (UCB) {
  127. this.onload = UCB;
  128. });
  129. }
  130. if (!("setCapture" in HTMLElement.prototype)) {
  131. U.M.HEPropert(HTMLElement.prototype, "setCapture", function () {
  132. return function () { window.captureEvents(Event.MOUSEMOVE); }
  133. },
  134. function () { });
  135. }
  136. if (!("releaseCapture" in HTMLElement.prototype)) {
  137. U.M.HEPropert(HTMLElement.prototype, "releaseCapture", function () {
  138. return function () {
  139. window.releaseEvents(Event.MOUSEMOVE);
  140. }
  141. },
  142. function () { });
  143. }
  144. if (!("onresize" in HTMLElement.prototype) || !browser.msie) {
  145. U.MB.onresize = function (UDOD, UDTD, UCB) {
  146. //Size事件设置
  147. if (UCB) {
  148. UDOD.contentDocument.defaultView.onresize = function () { U.M.apply(UDTD, [[UCB]])(); } //
  149. }
  150. else {
  151. try {
  152. delete this.__SizeElement__;
  153. } catch (e) { }
  154. }
  155. }
  156. U.M.HEPropert(HTMLElement.prototype, "onresize", function () {
  157. try {
  158. this.__SizeElement__.contentDocument.defaultView.onresize;
  159. } catch (e) {
  160. return null;
  161. }
  162. },
  163. function (UCB) {
  164. if (this.tagName) {
  165. //设置Set事件
  166. var _UDOD = this.__SizeElement__;
  167. (U.M.GetStyle(this, "position") == "static") && (this.style.position = "relative");
  168. if (!_UDOD || $(_UDOD).Parent() != this && UCB) {
  169. _UDOD = this.__SizeElement__ = $$("object", {
  170. "onload": [[U.MB.onresize, ["this", this, UCB]]],
  171. "type": "text/html",
  172. "data": "about:blank",
  173. "style": {
  174. "cssText": "display:block;position:absolute;top:0;left:0;height:100%;width:100%;overflow:hidden;pointer-events:none;z-index:-1;visibility:hidden;margin:0;padding:0;"
  175. }
  176. }, this)[0];
  177. }
  178. else {
  179. U.MB.onresize(_UDOD, this, UCB);
  180. }
  181. }
  182. });
  183. }
  184. if (!("componentFromPoint" in HTMLElement.prototype)) {
  185. U.M.HEPropert(HTMLElement.prototype, "componentFromPoint", function () {
  186. return function () {
  187. var _UE = U.M.GetMousep();
  188. var _UDE = U.M.GetElementAllInfo(this);
  189. if ((_UE["X"] > _UDE["CW"] + _UDE["TX"]) || (_UE["Y"] > _UDE["CH"] + _UDE["TY"])) {
  190. return "outside";
  191. }
  192. return "";
  193. }
  194. },
  195. function () { });
  196. }
  197. if (!("event" in window.constructor.prototype)) {/*检测浏览器是否支持鼠标键盘事件*/
  198. U.M.HEPropert(window.constructor.prototype, "event", function () {
  199. var _UDE = arguments.callee;
  200. while (_UDE.caller) {
  201. _UDE = _UDE.caller;
  202. }
  203. return _UDE.arguments ? _UDE.arguments[0] : null;
  204. }, function () { });
  205. }
  206. if (window.Event.prototype) {
  207. if (!("srcElement" in window.Event.prototype)) {
  208. U.M.HEPropert(window.Event.prototype, "srcElement", function () {
  209. return event.target;
  210. },
  211. function () { });
  212. }
  213. if (!("keyCode" in window.Event.prototype)) {
  214. U.M.HEPropert(window.Event.prototype, "keyCode", function () {
  215. return event.which;
  216. },
  217. function () { });
  218. }
  219. }
  220. }
  221. }
  222. U.M.QSEM; //防止选择器创建多个样式style
  223. /**
  224. * querySelectorAll 兼容
  225. *
  226. * @param {string} 搜索的内容
  227. * @param {element} 搜索的元素区域
  228. */
  229. document.querySelectorAll = document.querySelectorAll || function (USE, UOE) {
  230. var _UDTD = U.M.QSEM, //判断是否已经添加了上style识别样式
  231. _UDE = [],
  232. _UDOD = document.documentElement.firstChild,
  233. _UAE = (UOE || document).all, //搜索区域
  234. i = _UAE.length;
  235. try {
  236. //添加的style link
  237. _UDTD.styleSheet.cssText = USE + "{q:1}";
  238. }
  239. catch (e) {
  240. _UDTD = (U.M.QSEM = $$("style", {
  241. "type": "text/css",
  242. "styleSheet": {
  243. "cssText": USE + "{q:1}"
  244. }
  245. }, _UDOD));
  246. }
  247. //全局筛选
  248. while (i--) {
  249. (_UAE[i].currentStyle.q) && (_UDE.unshift(_UAE[i]));
  250. }
  251. return _UDE;
  252. };
  253. /**
  254. * querySelector 兼容
  255. *
  256. * @param {number} 搜索的内容
  257. */
  258. document.querySelector = document.querySelector || function (USE) {
  259. var _UDE = document.querySelectorAll(USE);
  260. return _UDE.length ? _UDE[0] : null;
  261. };
  262. /**
  263. * 选择元素区域 自定义搜索元素选择函数
  264. *
  265. * @param {number} 搜索的内容
  266. */
  267. window.SelectorElement = function (USE, UD) {
  268. var _UTF = (UD.id === "" && UD != document), _UTP = $$("div").querySelectorAll; //判断是否支持选择器
  269. if (_UTF) {
  270. UD.id = UD.uniqueID;
  271. }
  272. try { //选择器获取
  273. return (_UTP ? (UD || document) : document).querySelectorAll((UD.id ? ("#" + UD.id + " ") : " ") + USE, UD);
  274. }
  275. catch (e) {
  276. return null;
  277. }
  278. finally {
  279. (_UTF) && (UD.id = "");
  280. }
  281. }
  282. //#region 手机兼容处理,只要引用了uform框架,会自动使手机在滑动的过程中不会触发点击事件
  283. //Touch事件执行
  284. if ("ontouchstart" in document) {
  285. (U.M.OTS) && (U.M.ReEvent("touchstart", document, U.M.OTS, true));
  286. //touchstart
  287. U.M.AddEvent("touchstart", document, U.M.OTS = function () {
  288. var i, _UDOD = event.srcElement; U.M.UTX = null; U.M.StopBubble();
  289. U.M.ReEvent("touchstart", document, U.M.OTS, true); //移除start事件
  290. U.M.TouchE(_UDOD, "touchstart"); U.M.AddEvent("touchstart", document, U.M.OTS, true); //添加事件取消冒泡
  291. U.M.AddEvent("touchmove", document, U.M.OTSM); //添加move事件监听
  292. U.M.AddEvent("touchend", document, U.M.OTSE, true); //end事件等待
  293. }, true);
  294. U.M.OTSM = function () { //移动监视
  295. U.M.UTX = true; U.M.ReEvent("touchmove", document, U.M.OTSM); U.M.TouchE(event.srcElement, "touchmove"); U.M.StopBubble();
  296. }
  297. //执行toucastart
  298. U.M.OTSE = function (UDOD) {
  299. U.M.ReEvent("touchend", document, U.M.OTSE, true); U.M.StopBubble(); //移除事件
  300. if (!U.M.UTX) { U.M.TouchE(event.srcElement, "touchend"); } //事件执行
  301. }
  302. }
  303. //#endregion