jQuery 操作 XML(解決大小寫問題)

tiantangqiu發表於2014-07-14
jQuery操作xml時候,不同瀏覽器會產生大小寫不一致的問題, 參考了下網上一些的例子,寫的很簡單易懂, 簡單改了寫程式碼做個標記。


/**
* @description
* Operation of the XML
* @deprecated
* JQuery.js
* @version 1.0
* Date: 2013-09-16
*/
(function($) {
function mnpXml(opCode, xmlStr) {
return this.each(function() {
if (typeof xmlStr != "string") return;
if (!jQuery.isXMLDoc(this)) return;
var node = $.parseXML(xmlStr).firstChild.cloneNode(true);
switch (opCode) {
case "append":
this.appendChild(node);
break;
case "prepend":
if (this.childNodes.length > 0)
this.insertBefore(node, this.firstChild);
else
this.appendChild(node);
break;
case "after":
if (this.nextSibling)
this.parentNode.insertBefore(node, this.nextSibling);
else
this.parentNode.appendChild(node);
break;
case "before":
this.parentNode.insertBefore(node, this);
break;
}
});
}
$.fn.extend({
appendXml: function(s) {
return mnpXml.call(this, "append", s);
},
prependXml: function(s) {
return mnpXml.call(this, "prepend", s);
},
afterXml: function(s) {
return mnpXml.call(this, "after", s);
},
beforeXml: function(s) {
return mnpXml.call(this, "before", s);
},
xml: function() {
var elem = this[0];
return elem.xml || (new XMLSerializer()).serializeToString(elem);
},
innerXml: function() {
var s = this.xml();
var i = s.indexOf(">"), j = s.lastIndexOf("<");
if (j > i)
return s.substring(i + 1, j);
else
return "";
}
});
$.extend(jQuery, {
parseXml: function(xmlStr) {
if (window.ActiveXObject) {
var xd = new ActiveXObject("Microsoft.XMLDOM");
xd.async = false;
xd.loadXML(xmlStr);
return xd;
}
else if (typeof DOMParser != "undefined") {
var xd = new DOMParser().parseFromString(xmlStr, "text/xml");
return xd;
}
else return null;
}
});
})(jQuery);

相關文章