我正在使用的web应用程序是大约10年前构建的。它只能在IE中工作。我希望它在Crome中也能工作。
我在下一个问题中遇到:如何扩展IXMLDOMElement的"text“属性以使用"text”属性(使用IE时)或"textContent“属性(使用Crome时)。下面是一个代码示例。
Crome对下一行有一个问题:
var resultContent = result_xml.selectSingleNode("WSResult/Result").text; (*)不是"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;
//*************发布于 2013-02-13 23:45:00
看看here吧。
您必须定义getter:
返回函数() { Element.prototype.defineGetter('text',this.textContent;});
;)
https://stackoverflow.com/questions/14109071
复制相似问题