您可以使用
DocumentFragment模拟您的html并进行搜索,而无需将其附加到
DOM.
// Create your DocumentFragment to be able to work without DOM
var body_html = document.createDocumentFragment();
// Convert and append data from your jQuery to work with fragment
body_html.appendChild($(data)[0]);
// Now you can select using your jQuery
var $body_html = $(body_html);
// Now you can use the find or whatever you want, like if it was in the DOM
$body_html.find('.foo');
// Or you can append in your current document,
// but attention, after it the fragment reference is erased
$body_html.appendTo(document.body);
// now you need to get reference again from body,
// because your fragment doesn't exists anymore.
// So... if you try:
console.log(body_html); // undefined
console.log($body_html); // jquery over undefined, probably just a jquery useless
// At this point you will need to reference from DOM to continue manipulation
$body_html = $(document.body);
// Now I'm ready to continue the work
// This var is like your DocumentFragment, but already on DOM.
您也可以在jQuery中使用$(data).filter(‘.foo’)进行过滤,但正如您在此tests中所看到的,您的性能将会下降很多.