我可以使jQuery指向XML文档而不是DOM吗? [英] Can I make jQuery point to an XML document rather than the 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屋!