当元素以名称空间为前缀时,还 必须 添加名称空间:
.find('ns1:return')不起作用,因为:
jQuery用作伪选择器。.find('ns1:return')也不起作用,因为字符串中的单个反斜杠用作转义字符。"ns1:return"
变为"ns1:return"
与前一个相等。.find('ns1\:return')应该使用。双反斜杠用于转义冒号。
看来最后一种解决方案在IE和Firefox中可以正常工作,但在Opera,Chrome或Safari中则不能。为了获得最大的兼容性,请使用带有和不带有假前缀的jQuery选择器,即。
"ns1\:return,return"而不是平原
ns1\:return。
演示:http :
//jsfiddle.net/5BQjv/51/
// For example, this is the result:var data = '<ns1:executeResponse xmlns:ns1="http://sqlws.test.com">' + '<ns1:return>' + '<results> <row> ... </row> </results>' + '</ns1:return>' +'</ns1:executeResponse>';// The very first thing is to parse the string as XML. NOT later!var $xmlDoc = $($.parseXML(data));// Then, look for the element with the namespace:var $txt = $xmlDoc.find('ns1\:return, return');// No need to use unescape or something, just use DOM manipulation:// `results` is the immediate child. Don't use .find, but .childrenvar $firstrow = $txt.children("results").children(":first");您可能已经注意到,我为一些变量加了美元符号。按照惯例,为引用jQuery对象的变量加前缀美元符号是一种约定,以避免在开发期间/开发后产生混淆。



