<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Designsor &#187; Element</title>
	<atom:link href="http://www.designsor.com/tag/element/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designsor.com</link>
	<description>End Web Developer&#039;s Blog</description>
	<lastBuildDate>Sat, 14 Jan 2012 02:35:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Element in IE</title>
		<link>http://www.designsor.com/2010/07/28/element-in-ie/</link>
		<comments>http://www.designsor.com/2010/07/28/element-in-ie/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 09:53:19 +0000</pubDate>
		<dc:creator>xiaojue</dc:creator>
				<category><![CDATA[心情日志]]></category>
		<category><![CDATA[Element]]></category>
		<category><![CDATA[Element in IE]]></category>
		<category><![CDATA[javascript原生继承]]></category>

		<guid isPermaLink="false">http://www.designsor.com/?p=891</guid>
		<description><![CDATA[看过之前写的那篇BLOG，应该都发现了一句挺有意思的话。对于DOM中的element我可以添加自定义事件。比如： Element.prototype.show=function(){this.style.display=&#8217;block&#8217;;} 喔~然后调用之 var test=document.getElementById(&#8216;test&#8217;); test.show(); 恩，虽然在最后的最后我知道了这么写是严重的污染原型链的一种及其牛逼的反面例子。但是确实我今天要说的不是这个问题。 经过测试所有的浏览器除了IE6/7（没有测试什么360，遨游，TT什么的杯具浏览器……）其他的浏览器都对外开放Element对象，允许你对齐prototype的原型链上添加或者重写方法。 但是当你遇到了IE，那么你就也遇到了 “Element is undefine”&#8230;.这个错误了。 查了好久，具体的解决办法有2个，一个是在微软官方查到的用HTC的方法对Element进行扩展自定义方法，还有一种老外的方法，那么就是重写DOM方法。 显然，第2种方法在一些社区被受到推崇，号称可以解决99%的问题。而不用恶心人的HTC，而且不需要2个文件加载那么复杂。 那么到底是如何解决的呢？ 首先：Element is undefine，那么我们建立一个Element就好了。 var Element=function(){}; 恩 一个构造函数容器就建立了，他自身已经拥有了prototype属性了。因为是function嘛。 然后你每次给Element扩展方法的时候其实在IE6/7下都是给这个你自己定义的Element扩展的方法。那么我们如何调用这些扩展方法呢。 回想一下我们什么场合会用到事件。喔，getElementById,getElementsByTagName,createElement也就是这三种情况了。还有什么呢？原生的JS里只有这3种方法了吧？不对 只是99%，你可以.all也可以from[0]等等其他的方法获得元素，但是最基本其实就是这3个方法了。 那么我们要重写这3个DOM方法，在每次调用的时候都继承我们自己创建的Element对象下面的自定义方法。 如何去做呢？下面是完整的代码： (function(w, undefined){ var Element=w.Element &#124;&#124; function(){ return { author:&#8217;longxiao&#8217; }; }; if (!!Element().author) { var __createElement = document.createElement; document.createElement = function(tagName){ var element = __createElement(tagName); for (var key [...]]]></description>
			<content:encoded><![CDATA[<p>看过之前写的那篇BLOG，应该都发现了一句挺有意思的话。对于DOM中的element我可以添加自定义事件。比如：</p>
<p><span style="color: #ff0000;">Element.prototype.show=function(){this.style.display=&#8217;block&#8217;;}</span></p>
<p>喔~然后调用之</p>
<p><span style="color: #ff0000;">var test=document.getElementById(&#8216;test&#8217;);</span></p>
<p><span style="color: #ff0000;">test.show();</span></p>
<p>恩，虽然在最后的最后我知道了这么写是严重的污染原型链的一种及其牛逼的反面例子。但是确实我今天要说的不是这个问题。</p>
<p>经过测试所有的浏览器除了IE6/7（没有测试什么360，遨游，TT什么的杯具浏览器……）其他的浏览器都对外开放Element对象，允许你对齐prototype的原型链上添加或者重写方法。</p>
<p>但是当你遇到了IE，那么你就也遇到了 “Element is undefine”&#8230;.这个错误了。</p>
<p>查了好久，具体的解决办法有2个，一个是在微软官方查到的用HTC的方法对Element进行扩展自定义方法，还有一种老外的方法，那么就是重写DOM方法。</p>
<p>显然，第2种方法在一些社区被受到推崇，号称可以解决99%的问题。而不用恶心人的HTC，而且不需要2个文件加载那么复杂。</p>
<p>那么到底是如何解决的呢？</p>
<p>首先：Element is undefine，那么我们建立一个Element就好了。</p>
<p><span style="color: #ff0000;">var Element=function(){};</span></p>
<p>恩 一个构造函数容器就建立了，他自身已经拥有了prototype属性了。因为是function嘛。</p>
<p>然后你每次给Element扩展方法的时候其实在IE6/7下都是给这个你自己定义的Element扩展的方法。那么我们如何调用这些扩展方法呢。</p>
<p>回想一下我们什么场合会用到事件。喔，getElementById,getElementsByTagName,createElement也就是这三种情况了。还有什么呢？原生的JS里只有这3种方法了吧？不对 只是99%，你可以.all也可以from[0]等等其他的方法获得元素，但是最基本其实就是这3个方法了。</p>
<p>那么我们要重写这3个DOM方法，在每次调用的时候都继承我们自己创建的Element对象下面的自定义方法。</p>
<p>如何去做呢？下面是完整的代码：</p>
<p><span style="color: #ff0000;">(function(w, undefined){<br />
var Element=w.Element || function(){<br />
return {<br />
author:&#8217;longxiao&#8217;<br />
};<br />
};</span></p>
<p><span style="color: #ff0000;">if (!!Element().author) {<br />
var __createElement = document.createElement;<br />
document.createElement = function(tagName){<br />
var element = __createElement(tagName);<br />
for (var key in Element.prototype)<br />
element[key] = Element.prototype[key];<br />
return element;<br />
}</span></p>
<p><span style="color: #ff0000;">var __getElementById = document.getElementById;<br />
document.getElementById = function(id){<br />
var element = __getElementById(id);<br />
for (var key in Element.prototype)<br />
element[key] = Element.prototype[key];<br />
return element;<br />
}</span></p>
<p><span style="color: #ff0000;">var __getElementsByTagName = document.getElementsByTagName;<br />
document.getElementsByTagName = function(tagName){<br />
var elements = __getElementsByTagName(tagName);<br />
for (var key in Element.prototype){<br />
for(var i=0;i&lt;elements.length;i++){<br />
elements[i][key]=Element.prototype[key];<br />
}<br />
}<br />
return elements;<br />
}<br />
};</span></p>
<p><span style="color: #ff0000;">Element.prototype.getattrlength=function(){<br />
alert(this.attributes.length)<br />
};</span></p>
<p><span style="color: #ff0000;">document.getElementById(&#8216;page&#8217;).getattrlength();</span></p>
<p><span style="color: #ff0000;">})(window);</span></p>
<p>更多关于原型链的问题在我自己研究明白了之后再来分享……</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designsor.com/2010/07/28/element-in-ie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

