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');
精彩评论