开发者

jQuery - hide a element inside a html string

I have a ajax function that retrieves some data as html.

开发者_C百科

How can I hide a certain element from this html string? $(data).find(".element").hide() doesn't work...


Are you sure it doesn't work? A common mistake is to assume that the string itself is modified.

Try this instead:

var $data = $(data); // create new DOM elements, and keep a reference to them
$data.find(".element").hide();  // find and hide .element
$data.appendTo('wherever');  // append the new elements

Another possibility is that the .element is at the top level of the of the HTML that was returned.

If that's the case you'd need the filter()(docs) method instead of the find()(docs) method .

var $data = $(data); // create new DOM elements, and keep a reference to them
$data.filter(".element").hide();  // filter and hide .element
$data.appendTo('wherever');  // append the new elements

Last thing to try would be wrapping the entire HTML in a <div> element, then doing a .find().

// var $data = $('<div>' + data + '</div>');  // original version
var $data = $('<div>').append( data );   // this may be better. not sure.
$data.find(".element").hide();
$data.children().appendTo('wherever');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜