开发者

Element is not removed from html by jquery

开发者 https://www.devze.com 2023-03-16 23:21 出处:网络
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\"><h

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();
0

精彩评论

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