Converting HTML element to string in JavaScript / JQuery

前端 未结 5 468
一生所求
一生所求 2021-02-05 05:48

I would like to convert a html element created from a string back to the string after some modifications. But I get an empty string instead.

$(\'';
$('body').append(myHtmlString);

//as you noticed you can't just get it back
var myHtmlStringBack = $('#myFrame').html(); 
alert(myHtmlStringBack); // will be empty (a bug in jquery?) but...

//since an id was added to your iframe so you can retrieve its attributes back...
var width = $('#myFrame').attr('width');
var height = $('#myFrame').attr('height');
var src = $('#myFrame').attr('src');
var myReconstructedString = '';
alert(myReconstructedString);

提交回复
热议问题