Get HTML of div element returned by .get with jQuery

主宰稳场 提交于 2019-12-06 23:48:35
Felix Kling

$response.find('#main-images') will return an empty jQuery object. None of the selected elements has a descendant with ID main-images. Instead, one of the selected elements is the one you are looking for.

To get the a reference to the div use .filter() [docs] instead :

$response.filter('#main-images');

If you want to get the HTML, append the content to an empty div first and remove the unwanted elements:

var container = $('<div />').html(data);
container.children().not('#main-images').remove();
var html = container.html();

or use a outerHTML plugin:

var html = $response.filter('#main-images').outerHTML();

$(data) returns an array of elements and not an ordinary jQuery object. $(data)[1] contains your #main-images element.

As Felix Kling answered, you can use filter instead of find.

$(data).filter('#main-images').html();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!