当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Reading div content usig jQuery skips HTML, head and body tags

问题描述:

I am reading one HTML template and adding it to div (I have set runat='server') from server side code behind. This renders and shows HTML on page perfectly. Now I change the text within the rendered HTML on the client side and want to read updated text (complete HTML inside div) on the server side to update the template. So I have taken one hidden field and saving the updated HTML of div like this on the click of save button.

$('#btnSaveTemplate').click(function () {

$('#UpdatedEmailTemplate').val($('#divEmailBody').html());

});

HTML template:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

</head>

<body>

<p>

<strong><u>Database Decommission Notice – IMPORTANT NOTIFICATION</u></strong>

</p>

<p>

<strong>FOLLOWING DATABASE(S) ARE SCHEDULED TO BE DECOMMISSIONED in 4 WEEKS from {SentDate};</strong>

</p>

<p>

<strong>Why have you received this email?</strong>

</p>

<p>

You are receiving this email as because you are an ITAO/ITAO delegate/CSM/CSM-1/key technical contact for database(s) that are scheduled to be decommissioned by 30. Below are the applications you have been identified.

</p>

</body>

</html>

网友答案:

If it is template then do not add it to any html element like div.. use script tag instead.

<script id="UpdatedEmailTemplate" type="text/x-template">
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <p>
        <strong><u>Database Decommission Notice – IMPORTANT NOTIFICATION</u></strong>
    </p>
</body>
</html>
</script>

if you append template to div it doesn't stay as is and browser do its own adjustment to make page a valid html. in a page you cant have two html tags so browser automatically removes other one. same for body. HTML never throws error but it fixes errors by its own.. sometime fixes are not same as what we want. that'ts why its imp to have valid html

网友答案:

Using Javascript

var html = document.getElementsByTagName('html')[0];
var text = '<html>' + html.innerHTML + '</html/>';

Using jQuery:

var text = '<html>' + $('html').html() + '</html/>';
网友答案:

Thanks all for replies.

what I am doing now is reading only body tag from template and adding that to div therefore avoiding the issue.

分享给朋友:
您可能感兴趣的文章:
随机阅读: