数据过滤 html,javascript – 如何从ajax过滤返回数据的正文html?

您可以使用

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中所看到的,您的性能将会下降很多.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值