为XML原型函数的JavaScript定义原型属性
我正在使用此链接中提供的自定义javascript函数http://km0.la/js/mozXPath/ 在FireFox中实现特定的XML功能 代码如下:为XML原型函数的JavaScript定义原型属性,javascript,xml,prototype-programming,selectnodes,selectsinglenode,Javascript,Xml,Prototype Programming,Selectnodes,Selectsinglenode,我正在使用此链接中提供的自定义javascript函数http://km0.la/js/mozXPath/ 在FireFox中实现特定的XML功能 代码如下: // mozXPath // Code licensed under Creative Commons Attribution-ShareAlike License // http://creativecommons.org/licenses/by-sa/2.5/ if( document.implementation.hasFeatu
// mozXPath
// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ) {
if( typeof XMLDocument == "undefined" ) { XMLDocument = Document; }
XMLDocument.prototype.selectNodes = function(cXPathString, xNode) {
if( !xNode ) { xNode = this; }
var oNSResolver = this.createNSResolver(this.documentElement);
var aItems = this.evaluate(cXPathString, xNode, oNSResolver,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var aResult = [];
for( var i = 0; i < aItems.snapshotLength; i++) {
aResult[i] = aItems.snapshotItem(i);
}
return aResult;
}
XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode) {
if( !xNode ) { xNode = this; }
var xItems = this.selectNodes(cXPathString, xNode);
if( xItems.length > 0 ){ return xItems[0]; }
else{ return null; }
}
Element.prototype.selectNodes = function(cXPathString) {
if(this.ownerDocument.selectNodes) {
return this.ownerDocument.selectNodes(cXPathString, this);
}
else { throw "For XML Elements Only"; }
}
Element.prototype.selectSingleNode = function(cXPathString) {
if(this.ownerDocument.selectSingleNode) {
return this.ownerDocument.selectSingleNode(cXPathString, this);
}
else { throw "For XML Elements Only"; }
}
}
我想做的是将属性nodeTypedValue添加到selectSingleNode函数中,但我不确定如何执行此操作。在Element.prototype.selectSingleNode函数中,我尝试添加:
this.prototype.nodeTypedValue = this.textContent;
但是,它给了我一个错误,说它是未定义的。我甚至尝试在函数之外添加它,只是为了简化它并获得概念,它还说它是未定义的:
var XMLObj.selectSingleNode.prototype.nodeTypedValue = XMLObj.textContent;
alert(XMLObj.selectSingleNode("cd_rank").nodeTypedValue);
基本上,我想我要做的是,向一个原型函数添加一个原型属性。但是我需要一些帮助。如何添加nodeTypedValue,以便编写XMLObj.selectSingleNodePath.nodeTypedValue?好的,我想我已经知道如何在函数中添加它了,可能是因为运气而不是逻辑:
Element.prototype.selectSingleNode = function(cXPathString){
if(this.ownerDocument.selectSingleNode) {
var result = this.ownerDocument.selectSingleNode(cXPathString, this);
if (result != null) {
result.nodeTypedValue = result.textContent;
}
return result;
}
else{throw "For XML Elements Only";}
}
Element.prototype.selectSingleNode = function(cXPathString){
if(this.ownerDocument.selectSingleNode) {
var result = this.ownerDocument.selectSingleNode(cXPathString, this);
if (result != null) {
result.nodeTypedValue = result.textContent;
}
return result;
}
else{throw "For XML Elements Only";}
}