扩展“text"属性IXMLDOMElement在Chrome中导致工作

Extend "text" property of IXMLDOMElement to cause work in Chrome

本文关键字:Chrome 工作 IXMLDOMElement 属性 text quot 扩展      更新时间:2023-09-26

我正在使用的web应用程序是大约10年前构建的。它只在IE中工作。我希望它也能在罗马工作。

我在下一个问题中遇到:我如何扩展IXMLDOMElement的"文本"属性以使用"文本"属性(当使用IE时)或"textContent"属性(当使用Crome时)。下面是一个代码示例。

Crome下一行有问题:

var resultContent = result_xml.selectSingleNode("WSResult/Result").text;  (*)

用"textContent"属性代替"text"属性。我正在搜索如何扩展"文本"属性,它将在Crome工作也不改变(*)行。

我找到了下一个代码,但它不适合我

Element.prototype.text = function () {
return (this.textContent === undefined ? this.text : this.textContent);
} 

代码示例:

function XMLDocWorkspace
{
    this.LoadXML = function(xmlStr) {
        if (xmlStr == null || $.trim(xmlStr) == "") return null;       
        if (window.DOMParser) {
            parser = new DOMParser();
            _xmlDoc = parser.parseFromString(xmlStr, "text/xml");
        }
        else // Internet Explorer
        {
            _xmlDoc = new ActiveXObject(Msxml2.DOMDocument.6.0);
            _xmlDoc.async = false;
            _xmlDoc.loadXML(xmlStr);
        }
        var errorMsg = null;
        if (_xmlDoc.parseError && _xmlDoc.parseError.errorCode != 0) {
            errorMsg = "XML Parsing Error: " + _xmlDoc.parseError.reason
                          + " at line " + _xmlDoc.parseError.line
                          + " at position " + _xmlDoc.parseError.linepos;
        }
        else {
            if (_xmlDoc.documentElement) {
                if (_xmlDoc.documentElement.nodeName == "parsererror") {
                    errorMsg = _xmlDoc.documentElement.childNodes[0].nodeValue;
                }
            }
            else {
                errorMsg = "XML Parsing Error!";
            }
        }
        if (errorMsg) {
            return false;
        }        
    }
    this.SelectSingleNode = function(xPath) {
        if (_xmlDoc == null) return null;
        var nodes = this.selectNodes(xPath);
        if (nodes.length > 0) {
            return nodes[0];
        }
        return null;
    }

    this.SelectNodes = function (xPath)
    {
        if (_xmlDoc == null) return null;
        var condition = xPath;
        condition = condition.substring(0, 1) == "/" ? condition.substring(1, condition.length) : condition;
        condition = condition.replace(new RegExp("/", "g"), ">").replace(new RegExp("@", "g"), "");
        return $(_xmlDoc).find(condition);
    }
}

//*************************************************
 var result_xml = new XMLDocWorkspace()
 result_xml.loadXML(xmlString);
var resultContent = result_xml.selectSingleNode("WSResult/Result").text;
//*************

看这里

你必须定义getter:

Element.prototype。defineGetter('text', function(){返回this.textContent;});