【DOM编程艺术】document对象的write方法

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>document.write</title>
</head>

<body>
<script type="text/javascript">
document.write("<p>This is inserted</p>")
</script>
</body>
</html>

页面中会显示This is inserted

此种写法很容易导致验证错误。比如说,在第一个例子里,<script>标签后面的"<p>'很容易被认为是<p>标签,而在<script>标签的后面打开<p>标签是非法的。事实上,那个"<p>"和"</p>"只不过是一个将被插入文档的字符串的组成部分而已。

insertParagraph('This is inserted.');
function insertParagraph(text){
    var str='<p>';
    str += text;
    str += '</p>';
    document.write(str);
}

从某种意义上讲,使用document.write方法有点儿像使用<font>标签去设定字体和颜色。虽然这两种技术在HTML文档里的都工作的不错,但他们都不够优雅。

转载于:https://www.cnblogs.com/positive/p/3664477.html

猜你喜欢

转载自blog.csdn.net/weixin_33782386/article/details/93495760