您的位置:首页 > 编程语言

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

2014-04-14 17:31 169 查看
<!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文档里的都工作的不错,但他们都不够优雅。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: