栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用命名空间解析XML JQuery Ajax响应

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用命名空间解析XML JQuery Ajax响应

当元素以名称空间为前缀时,还 必须 添加名称空间:

  • .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对象的变量加前缀美元符号是一种约定,以避免在开发期间/开发后产生混淆。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/573606.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号