标签:
html部分
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<!--<link href="css.css" type="text/css" rel="stylesheet"/>-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--<link rel="shortcut icon" href="../image/web1.ico" type="images/x-icon"/>-->
<script language="JavaScript" src="_7.js" charset="UTF-8"></script>
<!--<script ></script>-->
</head>
<body>
<script>
//输出write()信息:把字符串写入到文档里面!
document.write(‘<p>这是写入的信息</p>‘)
insertText(‘外部写的write()信息‘)
</script>
<div id="testdiv">
<p> 这是div<em>my_id</em>组成的文本</p>
</div>
<div id="testdiv1">
</div>
</body>
</html>
---------------------
js部分
/**
* Created by Administrator on 2015/4/26 0026.
* utf8
*/
function insertText(text){
var str=‘<p>‘;
str+=text;
str+=‘</p>‘;
document.write(str);
}
window.onload=function(){
var testdiv=document.getElementById(‘testdiv‘);
alert(testdiv.innerHTML);
}
window.onload=function(){
var testdiv2=document.getElementById(‘testdiv1‘);
testdiv2.innerHTML=‘<p> is div<em>my_id</em>connect text</p>‘
}
//createElement方法:动态创建
window.onload=function(){
var para=document.createElement(‘p‘);
var info=‘nodeName:‘;
info+=para.nodeName;
info+=‘nodeType‘;
info+=para.nodeType;
alert(info);
}
//appendChild方法:添加到文档节点树上面,也就是让它成为这个文档某个现在节点的一个子节点
window.onload=function(){
var para=document.createElement(‘p‘);
var testdiv1=document.getElementById(‘testdiv1‘);
testdiv1.appendChild(para)
}
//createTextNode方法:创建文本节点信息
window.onload=function(){
var para=document.createElement(‘p‘);
var testdiv1=document.getElementById(‘testdiv1‘);
testdiv1.appendChild(para)
var text=document.createTextNode(‘new node:hello world!‘)
para.appendChild(text)
};
//appendChild还可以对那些尚未连接成为文档树一部分的节点使用,如下
window.onload=function(){
var para=document.createElement(‘p‘);
var text=document.createTextNode(‘new node:hello world11!‘);
para.appendChild(text);
var testdiv1=document.getElementById(‘testdiv1‘);
testdiv1.appendChild(para)
};
//效果与上面那个一样,所以2种都可以实现
//创建复杂的文本节点和元素节点
window.onload=function(){
var para=document.createElement(‘p‘);
var text1=document.createTextNode(‘this is:‘);
para.appendChild(text1);
var em=document.createElement(‘em‘);
text2=document.createTextNode(‘my‘);
em.appendChild(text2);
para.appendChild(em);
text3=document.createTextNode(‘connect is node.‘);
para.appendChild(text3);
var testdiv1=document.getElementById(‘testdiv1‘);
testdiv1.appendChild(para)
};
-----------------------------
标签:
原文地址:http://www.cnblogs.com/mhxy13867806343/p/4457804.html