日期:2014-05-16 浏览次数:20507 次
var HTMLElePrototype = {
extend: function ( name, fn ) {
var _getEleById = document.getElementById;
document.getElementById = function ( id ) {
var _ele = _getEleById.apply( document, [id] );
eval( "_ele." + name + "=fn" );
return _ele;
}
}
}
HTMLElePrototype.extend( "myPreviousSibling", function () {
var e = this.previousSibling;
while ( e && e.nodeTpye != 1 ) {
//我添加了e.nodeType的监视,发现这伙居然有两个值 如下.
//Name Value Type
//e.nodeType 1 Number
//e.nodeType undefined Undefined
e = e.previousSibling;
//这就是我上面判断失败的原因.为什么,我可从来没在C#里见过一个变量同时有两个值
//请高手解释下为什么
}
return e;
} );
window.onload = function x() {
var pr = document.getElementById( "p1" ).myPreviousSibling();
lalert( pr );
}
<body>
<p>p</p>
<p>p</p>
<p id="p1">p1</p>
<p>p</p></body>