日期:2014-05-16  浏览次数:20375 次

document.write("<p>hello word</p>");
document.write( " <p> hello   word </p> ");

document.createElement( 'p ')

有什么区别阿

------解决方案--------------------
效果不一样
document.write( " <p> hello word </p> ");运行后显示“hello word”
document.createElement( 'p ')只是创建了一个分段元素 <p> ,页面上不能显示
------解决方案--------------------
差别大得很!

document.write( " <p> hello word </p> "); // 仅仅是方法调用,字符串输出。

document.createElement( 'p ') // 创建了一个DOM 元素对象,或称为节点,其上拥用许多属性和方法!

如果要效果一致,代码如下:

document.write( " <p> hello word </p> ");

// 等效代码
var oParagraph = document.createElement( 'p ');
oParagraph.innerHTML = "hello word ";
document.body.appendChild(oParagraph);