jquery ajax innerhtml,javascript - JQuery html() vs. innerHTML - Stack Overflow

Specifically regarding "Can I rely completely upon jquery html() method that it'll perform like innerHTML" my answer is NO!

Run this in internet explorer 7 or 8 and you'll see.

jQuery produces bad HTML when setting HTML containing a

tag nested within a

tag where the beginning of the string is a newline!

There are several test cases here and the comments when run should be self explanatory enough. This is quite obscure, but not understanding what's going on is a little disconcerting. I'm going to file a bug report.

$(function() {

// the following two blocks of HTML are identical except the P tag is outside the form in the first case

var html1 = "

";

var html2 = "

";

//

tag nested within

RunTest("

tag nested within

tag", html1); // succeeds in Internet Explorer

RunTest("

tag nested within

tag with leading newline", "\n" + html1); // fails with added new line in Internet Explorer

//

tag nested within

RunTest("

tag nested within

tag", html2); // succeeds in Internet Explorer

RunTest("

tag nested within

tag with leading newline", "\n" + html2); // succeeds in Internet Explorer even with \n

});

function RunTest(testName, html) {

// run with jQuery

$("#placeholder").html(html);

var jqueryDOM = $('#placeholder').html();

var jqueryFormSerialize = $("#placeholder form").serialize();

// run with innerHTML

$("#placeholder")[0].innerHTML = html;

var innerHTMLDOM = $('#placeholder').html();

var innerHTMLFormSerialize = $("#placeholder form").serialize();

var expectedSerializedValue = "field1=111&field2=222";

alert( 'TEST NAME: ' + testName + '\n\n' +

'The HTML :\n"' + html + '"\n\n' +

'looks like this in the DOM when assigned with jQuery.html() :\n"' + jqueryDOM + '"\n\n' +

'and looks like this in the DOM when assigned with innerHTML :\n"' + innerHTMLDOM + '"\n\n' +

'We expect the form to serialize with jQuery.serialize() to be "' + expectedSerializedValue + '"\n\n' +

'When using jQuery to initially set the DOM the serialized value is :\n"' + jqueryFormSerialize + '\n' +

'When using innerHTML to initially set the DOM the serialized value is :\n"' + innerHTMLFormSerialize + '\n\n' +

'jQuery test : ' + (jqueryFormSerialize == expectedSerializedValue ? "SUCCEEDED" : "FAILED") + '\n' +

'InnerHTML test : ' + (innerHTMLFormSerialize == expectedSerializedValue ? "SUCCEEDED" : "FAILED")

);

}

This is #placeholder text will

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值