【问题标题】:Defining prototype property for JavaScript for XML prototype functions为 XML 原型函数定义 JavaScript 的原型属性
【发布时间】:2011-06-23 16:14:42
【问题描述】:

我正在使用此链接 (http://km0.la/js/mozXPath/) 提供的自定义 javascript 函数在 FireFox 中实现特定的 XML 功能。

代码如下:

// 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"; }
    }
}

假设 XML 对象已经定义并加载了 XML 内容,下面是一个如何访问名为“cd_rank”的 XML 标记的示例:

var cd_rank_XMLObj = XMLObj.selectSingleNode("cd_rank");

我想要做的是将属性“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.selectSingleNode(Path).nodeTypedValue”?

【问题讨论】:

    标签: javascript xml prototype-programming selectnodes selectsinglenode


    【解决方案1】:

    好的,我想我想出了如何在函数中添加它,可能更多的是因为运气而不是逻辑:

    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";}
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多