我可以使jQuery指向XML文档而不是DOM吗? [英] Can I make jQuery point to an XML document rather than the DOM?

查看:59
本文介绍了我可以使jQuery指向XML文档而不是DOM吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试翻页以从XML而不是从DOM读取,我宁愿不要在每次调用时都使用$(xml).find('blah'),而且我想知道是否有一种将jQuery指向xml而不是DOM的方法.

I'm trying to retrofit a page to read from XML rather than the DOM, and I'd rather not use $(xml).find('blah') for every single call, and I was wondering if there was a way to point jQuery at xml, rather than the DOM.

例如,我希望能够使用jQuery方法导航xhtml: $('blah').What

For instance, I'd like to be able to navigate my xhtml with the jQuery method as such: $('blah').whatever

这可能吗?

推荐答案

不,您不能重定向全局document对象以指向您创建的任意DOM文档.

No, you cannot redirect the global document object to point to an arbitrary DOM document you've created.

jQuery为$()函数提供了 context参数为此,因此您应该使用它:

jQuery has given you the context argument to the $() function for exactly this purpose, so you should just use that:

$('blah', xmlDoc).whatever()

或者,您也可以将其存储在var中以减少重复,然后从那里向下遍历:

Or you can store it in a var to reduce repetition if you like, and traverse downwards from there:

var $xml = $(xmlDoc);
$xml.find('blah').foo();
$xml.find('bloh').bar();

这篇关于我可以使jQuery指向XML文档而不是DOM吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆