i have this code and its not working
var data = '<div class="myform" id="stylized"><开发者_开发百科;form enctype="multipart/form-data" method="post" action="" id="form1"><h1>Account form</h1></form></div>';
$(data).find('h1').remove();
alert(data);
here h1 is still there in result
Well, you're just updating the jQuery wrapped set
, not the data variable
. You need to assign the new formed jQuery set and access it's .html()
method to get the result:
var data = '<div class="myform" id="stylized"><form enctype="multipart/form-data" method="post" action="" id="form1"><h1>Account form</h1></form></div>';
var $myData = $(data).find('h1').remove().end();
alert($myData.html());
You're taking a string and building a DOM structure from it (that's what $(data)
does). You're then modifying that DOM structure (with the remove
) call. That change affects the DOM structure. It does not affect the original string.
If you want the string back out again, you'll have to retrieve it.
$data = $(data);
$data.find('h1').remove();
data = $data.html();
精彩评论